gpt4 book ai didi

r - 如何在R中的数据框的同一列中显示项目之间的共享特征

转载 作者:行者123 更新时间:2023-12-04 11:16:25 25 4
gpt4 key购买 nike

如果数据框包含项目列、它们的特征及其特征值,如何在数据框的同一列中显示项目之间的共享特征?

这是输入数据框

df <- data.frame(group = c(rep(c('A', 'B', 'C'), 3)),
feature = c('x','x','x','y','y','z','z','w','t'),
value=c(1,2,1,3,2,1,2,2,3))

这是所需输出的示例,它比较了任意两对 A&B、A&C、B&C 和过滤器的共同特征:
df_desired <- data.frame(group1 =c('A','A','B'), group2 = c('B','C','C'), shared_feature = c('x','x','x'), value1 = c(1, 1,2), value2 = c(1, 2,1))

最佳答案

您可以尝试以下操作:

# Added stringsAsFactors=F argument
df <- data.frame(group = c(rep(c('A', 'B', 'C'), 3)),
feature = c('x','x','x','y','y','z','z','w','t'),
value=c(1,2,1,3,2,1,2,2,3),stringsAsFactors = F)

df_desired <- data.frame(group1 =c('A','A','B'), group2 = c('B','C','C'), shared_feature = c('x','x','x'), value1 = c(1, 1,2), value2 = c(1, 2,1))

# For rbindlist function
library(data.table)

# Keep only features that are available for every group
df_agg = aggregate(value ~ feature, data = df, FUN = length)
shared_feats = df_agg$feature[df_agg$value==length(unique(df$group))]
df = df[df$feature %in% shared_feats,]

# A function that takes a feat_df containing the values of one feature for each group,
# and converts it to our expected output.
create_comb_df <- function(feat_df)
{
df2 = as.data.frame(t(combn(feat_df$group,2)))
colnames(df2) = c('group1','group2')
df2$feature = feat_df$feature[1]
df2$value1 = feat_df$value[match(df2$group1,feat_df$group)]
df2$value2 = feat_df$value[match(df2$group2,feat_df$group)]
return(df2)
}

# Create final output
rbindlist(lapply(split(df,as.character(df$feature)),create_comb_df))

输出:
   group1 group2 feature value1 value2
1: A B x 1 2
2: A C x 1 1
3: B C x 2 1

或者,要获取所有共享功能,请替换
shared_feats = df_agg$feature[df_agg$value==length(unique(df$group))]


shared_feats = df_agg$feature[df_agg$value>1]

结果是:
   group1 group2 feature value1 value2
1: A B x 1 2
2: A C x 1 1
3: B C x 2 1
4: A B y 3 2
5: C A z 1 2

希望这可以帮助!

关于r - 如何在R中的数据框的同一列中显示项目之间的共享特征,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49746826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com