How to group data by an id in R

3

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 value of the field id , to solve this I can average the values of field x that belongs to the same id .

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

    
asked by anonymous 12.04.2015 / 19:19

2 answers

3

There are several ways to do this in R, what you want is to add a variable using another as a group. This SOpt question speaks exactly to this and will help you (#).

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

Rebuilding data:

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

Using 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
    
13.04.2015 / 01:45
2

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 manipulating dataframes):

library(dplyr)
df %>% group_by(id) %>% summarise(x=mean(x))
#mesmo resultado
    
13.04.2015 / 01:44