How to group data by an id in R

Asked

Viewed 1,551 times

4

I have the following database:

id     x
1      2
1      3
2      3
3      3
3      3
3      3

I wanted to create a new database without repeating the field value id, to solve this I can average the field values x that belongs to the same id.

My question is: How can I do this in R?

2 answers

4

There are several ways to do this in R, what you want is to aggregate one variable using another as a group. This question of Sopt speaks exactly about it and will help you ( How to consolidate (aggregate or group) values in a database? ).

But giving an answer to your specific case, different from the molx answer.

Recreating the data:

dados <- read.table(text = "
id     x
1      2
1      3
2      3
3      3
3      3
3      3", header = TRUE)

Using the data.table:

library(data.table)
dados <- data.table(dados)
dados[ , list(x = mean(x)), by = id]
 id   x
1:  1 2.5
2:  2 3.0
3:  3 3.0

3

You can do in base with the function aggregate:

df <- data.frame(id=c(1, 1, 2, 2, 3, 3), x=c(2, 3, 3, 3, 3, 3))
aggregate(x~id, df, FUN=mean)
#  id   x
#1  1 2.5
#2  2 3.0
#3  3 3.0

With dplyr (a very useful package for handling dataframes):

library(dplyr)
df %>% group_by(id) %>% summarise(x=mean(x))
#mesmo resultado
  • 1

    Oops, we answered at the same time exactly the same thing! I will change my answer.

  • 1

    Yeah, after six hours we answered the same thing 30 seconds apart, hahaha.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.