简体   繁体   中英

subset columns with common values in long data frame

I have the following data frame:

Group 1 ID A    Value
Group 1 ID B    Value
Group 1 ID C    Value
Group 2 ID B    Value
Group 2 ID C    Value
Group 3 ID B    Value
…   …   …

I am trying to use dplyr to get the mean value for each of the same ID across groups (eg the mean of the value of ID B across group 1, group 2, and group 3). However, not every group has all of the IDs so I wanted to subset so that only means for IDs which are in all groups get computed. I know that I can group_by(dataFrame, group) %>% filter subset %>% group_by(id) %>% mutate(mean) but I don't know what code to place in the filter subset.

How about

df %>%
  group_by(id) %>%
  mutate(count  = n()) %>%
  filter(count != ngroups) %>% #...

So basically remove all the rows in the dataframe that correspond to an ID that doesn't appear in all groups, then perform the computation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM