Skip to content

Instantly share code, notes, and snippets.

@geofferyzh
Created April 17, 2012 19:49
Show Gist options
  • Save geofferyzh/2408580 to your computer and use it in GitHub Desktop.
Save geofferyzh/2408580 to your computer and use it in GitHub Desktop.
RinAction - R Data Manipulation - Reshaping Data
#####################################################
# -------- Aggregation and Restructuring -----------#
#####################################################
############
#Transpose
############
cars <- mtcars[1:5, 1:4]
t(cars)
####################################
# Aggregating data (group by)
####################################
options(digits=3)
attach(mtcars)
agg <- aggregate(mtcars, by=list(cyl, gear), FUN=mean, na.rm=TRUE)
agg # note that the by variables must be in a list (even if there's only one)
############
# Reshape
############
install.packages("reshape")
library(reshape)
id <- c(1,1,2,2)
time <-c(1,2,1,2)
x1 <- c(5,3,6,2)
x2 <- c(6,5,1,4)
mydata <- data.frame(id, time, x1, x2, stringsAsFactors =FALSE)
# Melting
md <- melt(mydata, id=(c("id","time")))
#Casting
cast(md, id~variable, mean)
cast(md, time~variable, mean)
cast(md, id~time, mean)
cast(md, id+time~variable)
cast(md, id+variable~time)
cast(md, id~variable+time)
# Using Cast() to Transpose Rows to Columns
head(data)
# Package User Installed
# 1 abind 1 1
# 2 AcceptanceSampling 1 0
# 3 ACCLMA 1 0
# 4 accuracy 1 1
# 5 acepack 1 0
# 6 aCGH.Spline 1 0
data.transpose <- cast(data, User ~ Package, value = 'Installed')
head(data[,1:10])
# Rpkg_1 Rpkg_2 Rpkg_3 Rpkg_4 Rpkg_5 Rpkg_6 Rpkg_7 Rpkg_8 Rpkg_9 Rpkg_10
# RUser_1 1 1 0 0 1 0 0 1 0 1
# RUser_2 3 1 1 0 1 1 1 0 1 1
# RUser_3 4 0 1 1 1 1 0 0 1 0
# RUser_4 5 1 1 1 0 1 0 1 1 1
# RUser_5 6 1 1 1 0 1 0 1 1 1
# RUser_6 7 1 1 1 0 1 1 1 1 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment