Wide to long in r | Reshape Data Frame in R

Опубликовано: 06 Январь 2021
на канале: Coder's Digest
7,513
38

How to reshape a data frame from wide to long in r, day wide to long in r or long to wide format. More details:
code:
library(reshape2)

cars =mtcars

converting from wide to long format
carsmelt = melt(cars, id.vars = c("mpg", "cyl"))

head(carsmelt)
tail(carsmelt)


revert back from long to wide data
carswide_format = dcast(carsmelt, mpg + cyl ~ variable)
head(carswide_format)


carsmelt2 = melt(cars)