汇总具有相同ID的行

汇总具有相同ID的行

本文介绍了汇总具有相同ID的行,并仅保留R中的唯一条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是R的初学者。我在R中有如下数据框:

I am a beginner in R. I have data frame in R as follows:

 Id          Values
A_0_d   Low_5524; Low_6412; Hi_50567
A_0_d   Low_5509; Low_6412; Low_6897; Hi_16021
A_0_d   Low_5524; Low_4930; Low_5886
B_1_d   Low_3697; Low_4519; Low_5524
C_3_d   Low_5576; Low_5581
C_3_d   Hi_30246
C_3_d   Low_5576; Hi_30246

我想基于ID汇总数据帧,即将相同ID的所有值分组到一行并且仅保留如下所示的唯一条目:

I would like aggregate the data frame based on the Ids i.e. group all the values of same ID in a single row and retain only unique entries like follows:

A_0_d   Low_5524; Low_6412; Hi_50567; Low_5509; Low_6897; Hi_16021; Low_4930; Low_5886
B_1_d   Low_3697; Low_4519; Low_5524
C_3_d   Low_5576; Low_5581; Hi_30246

我可以利用集合函数吗?请指导我。

Can I make use of aggregate function.Kindly guide me.

推荐答案

将 data.frame转换为 data.table( setDT(df1))。然后,在按 Ids分组后,将,将取消列出输出,获得 unique 元素和 paste 一起

Convert the 'data.frame' to 'data.table' (setDT(df1)). Then, split the 'Values' by "; " after grouping by 'Ids', unlist the output, get the unique elements and paste it together

library(data.table)
setDT(df1)[, .(Values = paste(unique(unlist(strsplit(Values, "; "))),
                                                 collapse="; ")), by = Id]
#   Id
#1: A_0_d
#2: B_1_d
#3: C_3_d
#                                                                           Values
#1: Low_5524; Low_6412; Hi_50567; Low_5509; Low_6897; Hi_16021; Low_4930; Low_5886
#2:                                                   Low_3697; Low_4519; Low_5524
#3:                                                   Low_5576; Low_5581; Hi_30246

这篇关于汇总具有相同ID的行,并仅保留R中的唯一条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 18:33