Reputation: 4328
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
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
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