R : Summarize Data

Deepanshu Bhalla 1 Comment
This tutorial explains how to aggregate or summarize data.

Create a sample data
set.seed(1)
data <- data.frame(X = paste("s", sample(1:3, 15, replace = TRUE), sep = ""),Y = ceiling(rnorm(15)), Z = rnorm(15), A = rnorm(15), B = rnorm(15))
Sample Data
Calculate Mean of Z by grouping variable X
dat1 = aggregate(Z ~ X, data=data, FUN=mean)
Calculate Mean of Z by 2 grouping variables
dat2 = aggregate(Z~ X + Y, data=data, FUN=mean)
Calculate Mean of Y and Z by grouping variable X
dat3 = aggregate(cbind(Y,Z)~X, data=data, FUN=mean)
Calculate Mean of all the variable by grouping variable X
dat4 = aggregate(.~X, data=data, FUN=mean)

Concatenate Text Based on Criteria
testDF <- data.frame(v1 = c(1,3,5,7,8,3,5,NA,4,5,7,9),
                     v2 = c(11,33,55,77,88,33,55,25,44,55,77,99) )
aggregate(v2 ~ v1, data = testDF, FUN=paste, sep=",")
Related Posts
Spread the Word!
Share
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

Post Comment 1 Response to "R : Summarize Data"
  1. Hi, btw what does ceiling in rnorm (ceiling(rnorm(15)) means?

    ReplyDelete
Next → ← Prev