Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
breakoutModewide
breakoutWidth760
dataf <- read.table("walrus_sounds.tsv", header = F, sep = "\t")
colnames(dataf) <- c('name', 'vocalization', 'min_sec')
sec1 <- as.numeric(str_split_i(dataf$min_sec, ':', 1))*60 
sec2 <- as.numeric(str_split_i(dataf$min_sec, ':', 2))

#Base R
dataf$seconds <- sec1+sec2
mean_by_group_baseR <- aggregate(seconds ~ name + vocalization, data = dataf, FUN = mean)
mean_by_group_baseR_jocko <- mean_by_group_baseR[mean_by_group_baseR$name == 'Jocko',]
jocko_seconds <- mean_by_group_baseR_jocko$seconds

#Dplyr
mean_by_group_dplyr_jocko <- dataf %>%
    mutate(seconds = sec1+sec2) %>%
    group_by(name,vocalization) %>%
    summarize(Mean_seconds = mean(seconds)) %>%
    filter(name=="Jocko") %>% 
    as.data.frame()

#if you want to extract a column into a vector, use pull()
jocko_seconds <- mean_by_group_dplyr_jocko %>% pull(Mean_seconds)

#option, left merge
Note

We sent the same dataframe into a series of base R manipulations on the one hand, and into a dpyler dplyer pipe expression on the other. What we ended up with were a dataframe for the one output and a tibble for the other output. You should know at this point realize that a tibble is different from a dataframe. A tibble , although I won’t get into exactly how. However, a tibble is the dataframe structure used in the tidyverse, and it will not behave like a dataframe , so when attempting to use base R functions on it. Therefore, use as.data.frame(tibble) to recover a dataframe.

...