简体   繁体   English

长数据框中具有公共值的子集列

[英]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). 我正在尝试使用dplyr获取组中每个相同ID的平均值(例如,组1,组2和组3中ID B的平均值)。 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. 但是,并非每个组都具有所有ID,因此我想对它进行子集化,以便只计算所有组中的ID。 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. 我知道我可以group_by(dataFrame, group) %>% filter subset %>% group_by(id) %>% mutate(mean)但是我不知道要在过滤器子集中放置什么代码。

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. 因此,基本上删除数据框中与未出现在所有组中的ID对应的所有行,然后执行计算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有列之一倒数第二个值的数据帧的子集 - Subset of a data frame with the penultimate values of one of the columns 通过基于两列随机选择值来子集数据帧 - Subset a data frame by randomly selecting values based on two columns 使用 R 中的匹配将列子集乘以第二个数据框中的值 - Multiply subset of columns by values in second data frame using match in R R - 如何根据数据框中一行中的值创建列的子集 - R - How to make a subset of columns based on values in a row in a data frame 两列具有特定不同值时的子集数据框 - Subset data frame when two columns have specific different values 子集来自数据帧的不匹配行,该行由第二个数据帧中的多列中的值限制 - Subset unmatched rows from data frame conditoned by values in multiple columns in a second data frame 如何通过仅采用此数据帧中2列的非NA值来对数据帧进行子集化 - How to subset a data frame by taking only the Non NA values of 2 columns in this data frame 检查 data.frame 列中的所有值是否都是用于子集虚拟变量的整数,又名列中的所有值是否为 TRUE? - check if all values in data.frame columns are integers to subset dummy variables aka are all values in a column TRUE? 在R中交换(选定/子集)数据帧列 - Swap (selected/subset) data frame columns in R 如何通过配对列来对数据框进行子集化 - How to subset a data frame by pairing its columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM