Reshape Data Frame from Wide to Long Format in R (2 Examples) | melt & gather Functions in RStudio
Автор: Statistics Globe
Загружено: 2019-12-25
Просмотров: 35335
How to reshape a data frame from wide to long format in R. More details: https://statisticsglobe.com/reshape-d...
R code:
##### Example data
data_wide <- data.frame(ID1 = c("A", "A", "B", "C", "B"), # Create example data
ID2 = c("b", "c", "c", "a", "d"),
x = 1:5,
y = 6:10)
##### Example 1
install.packages("reshape2") # Install reshape2
library("reshape2") # Load reshape2
data_long1 <- melt(data_wide, # Apply melt function
id.vars = c("ID1", "ID2"))
##### Example 2
install.packages("tidyr") # Install tidyr
library("tidyr") # Load tidyr
data_long2 <- data_wide %>% # Apply gather function
gather(variable, value, - c(ID1, ID2))
Доступные форматы для скачивания:
Скачать видео mp4
-
Информация по загрузке: