...
| Code Block | ||||
|---|---|---|---|---|
| ||||
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, and into a dpyler pipe expression. What we ended up with were a dataframe and a tibble. You should know at this point that a tibble is different from a dataframe. A tibble will not behave like a dataframe, so use as.data.frame(tibble) to recover a dataframe. |
...
| Code Block | ||||
|---|---|---|---|---|
| ||||
mtcars_sub <- head(mtcars)
library(tidyverse)
mtcars_long <- mtcars_sub %>%
rownames_to_column(var = "model") %>% # Convert row names (car models) to a column
pivot_longer(
cols = -c(mpg), # Select all columns except 'mpg, model' to pivot
names_to = "variable", # New column for the original column names
values_to = "value" # New column for the values from the original columns
)
|
...
| Code Block | ||||
|---|---|---|---|---|
| ||||
library(data.table)
mtcars_sub$model <- rownames(mtcars_sub)
mtcars_long <- melt(
data.table(mtcars_sub),
id.vars = c("model", "mpg"),
variable.name = 'variable',
measure.name = colnames(mtcars_sub)[colnames(mtcars_sub) != 'mpg'] #additional
methods )
|
| Info |
|---|
ggplot2 is very powerful |
...
Full (outer):
...
Inner:
...
Left:
...
Right:
...
Code Block
| breakoutMode | wide |
|---|---|
| breakoutWidth | 760 |