Reshape Data Frame from Wide to Long Format in R (2 Examples) | melt & gather Functions in RStudio

Описание к видео Reshape Data Frame from Wide to Long Format in R (2 Examples) | melt & gather Functions in RStudio

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))

Комментарии

Информация по комментариям в разработке