tallowen
tallowen

Reputation: 4328

Reduce a table in R

I have a table that has a format along the lines of

user  data
1234  42
1234  51
1234  50
1235  61
1235  55

I want to create a histogram of the average "data" based on user. In other words, I would like to combine rows of the table with the same userID and come up with an average for them. How would I do this on a large dataset?

Upvotes: 2

Views: 1278

Answers (3)

Waldir Leoncio
Waldir Leoncio

Reputation: 11341

First, let's reproduce your data:

user <- c(rep("1234", 3), rep("1235", 2))
data <- c(42, 51, 50, 61, 55)
df   <- data.frame(user, data)

Now, for a one-line solution:

aggregate(data ~ user, df, mean)

Upvotes: 1

Tomas
Tomas

Reputation: 59505

user_mean = tapply(data, user, mean)
hist(user_mean, breaks = 30)

Upvotes: 1

user554546
user554546

Reputation:

Just use tapply (assuming that your original data frame is named dat):

avgdata=tapply(dat$data,dat$user,FUN=mean)
hist(avgdata)

Upvotes: 5

Related Questions