Copy and paste the following code to your R Studio platform or R version:

# Aggregate by a combination of variables
a <- aggregate(weight ~ Chick + Diet, data = ChickWeight, FUN = mean)
a
a[order(a$Chick, a$Diet), ]

# When you convert a factor, convert it first to a character and then to a numeric
str(a)
a$Chick <- as.numeric(as.character(a$Chick))
str(a)

# Now we can sort it properly
a[order(a$Chick, a$Diet), ]

# A mini challenge
# Find at what time period the mean chick weight exceeds 150
head(ChickWeight)

mean.w.time <- aggregate(weight ~ Time, data = ChickWeight, FUN = mean)
mean.w.time
mean.w.time[mean.w.time$weight > 150, ]
# Since we want the exact time period we need to further specify the statement to return a row
mean.w.time[mean.w.time$weight > 150, ][1, ]

# De-aggregate and perform cross tabulations
# install.packages(“reshape2”)
library(reshape2)
library(ggplot2)

head(economics)
melted <- melt(economics, id = “date”)
head(melted)
tail(melted)

# Let’s create excel-like pivot cross tabulations
head(french_fries)
a <- dcast(french_fries, treatment ~ subject,
value.var = “potato”,
fun.aggregate = function(x) {mean(x, na.rm = TRUE)})
head(a)

Posted by Dimitris

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s