Reshape Data Form in R

Using reshape2 package functions:

  • dcast: From long to wide
  • melt: From wide to long

Example:

# Get data
library(reshape2)
library(dplyr)  # For select function
data("airquality")
dat <- airquality %>%
      filter(Wind>15)    # This is a wide form data
# Convert it to long
longdat <- melt(dat)     # ALl variables are in a column and their value in another
longdat <- melt(dat, id = c('Month', 'Day'))  # Remain the variable Month and Day 
# Convert back to wide
widedat <- dcast(longdat, Month + Day ~ variable)

你可能感兴趣的:(R,coursera,data,science)