akash ansari
akash ansari

Reputation: 66

Combining rows with repeated ids by means in R

I would like to combine the following dataframe where repeated ids will be combined together by the mean values of repeated observation.

id     V1      V2
AA  21.76410    1       
BB  25.57568    0       
BB  20.91222    0       
CC  21.71828    1       
CC  22.89878    1       
FF  22.20535    0   
structure(list(id = structure(c(1L, 2L, 2L, 3L, 3L, 4L), .Label = c("AA", 
"BB", "CC", "FF"), class = "factor"), V1 = c(21.7640981693372, 
25.575675904744, 20.9122208946358, 21.7182828011676, 22.8987775530191, 
22.2053520672232), V2 = c(1, 0, 0, 1, 1, 0)), class = "data.frame", row.names = c(NA, 
-6L))

After data reduction by mean, it should like this-

id     V1       V2
AA  21.76410    1       
BB  23.24395    0   # mean reduction for BB in V1 and V2    
CC  22.30853    1   # same as above 
FF  22.20535    0   
structure(list(id = structure(1:4, .Label = c("AA", "BB", "CC", 
"FF"), class = "factor"), V1 = c(21.7641, 23.24395, 22.30853, 
22.20535), V2 = c(1, 0, 1, 0)), class = "data.frame", row.names = c(NA, 
-4L))

How can I do that in R? Any package function or custom function code you want to share with me would be of great help.

Thanks.

Upvotes: 3

Views: 54

Answers (3)

ThomasIsCoding
ThomasIsCoding

Reputation: 101139

Another way of using aggregate from base R

dfout <- aggregate(df[-1],df[1],FUN = mean)

such that

> dfout
  id       V1 V2
1 AA 21.76410  1
2 BB 23.24395  0
3 CC 22.30853  1
4 FF 22.20535  0

Upvotes: 2

sm925
sm925

Reputation: 2678

Using dplyr:

df %>%
  group_by(id) %>%
  mutate(V1 = ifelse(n() > 1, mean(V1), V1)) %>%
  unique()

# A tibble: 4 x 3
# Groups:   id [4]
#   id       V1    V2
#<fct> <dbl> <dbl>
#1 AA     21.8     1
#2 BB     23.2     0
#3 CC     22.3     1
#4 FF     22.2     0

Upvotes: 2

akrun
akrun

Reputation: 886968

With base R, it can be done with aggregate

aggregate(.~ id, df1, mean)

Upvotes: 4

Related Questions