如何将数据框的行总结为一个,同时删除R中的重复项?

发布于 2025-01-22 01:45:42 字数 803 浏览 0 评论 0原文

因此,我有一个具有2行或更多行和不同列(ID,位置,任务,技能等)的数据框架。我想将这些行汇总到(a)一行(dataframe)中,其中应将不同的列条目连接在一起(但是只有不同的是,如果对于两个行,ID是相同的,则最终的dataFrame行应仅显示一个ID不显示一个ID相同的两次IE“ ID1”,但是如果它们不同,则应显示两个ID1,ID2”),应将某些数值值(+)添加在一起。

df = data.frame("ID" = c(PA1, PA1), "Occupation" = c("PO - react to DCS, initiate corrective measures,  react to changes
", "PO - data based operations"), "Field" = c("PA","PA"), "Work" = c(0.5, 0.1), "Skill1" = c(CRO, CRO), "Skill2" = c(0, PPto), "ds" = c(5, 5))
print(df)

输出应该看起来像这样,

df_final = data.frame("ID" = c(PA1), "Occupation" = c("PO - react to DCS, initiate corrective measures,  react to changes, data based operations"), "Field" = c("PA"), "Work" = c(0.6), "Skill1" = c(CRO), "Skill2" = c(PPto), "ds" = c(5))
print(df_final)

谢谢!

so, I have a data frame with 2 or more rows and different columns (ID, Location, Task, Skill, ...). I want to summarize these rows into (a) one row (dataframe) where different column entries should be joined together (but only if different! i.e. if for two rows the IDs are the same, the final dataframe row should show only one ID not the same twice i.e. "ID1", but if they are different, both should be shown i.e. 'ID1, ID2") and some numerical values should be added (+) together.

df = data.frame("ID" = c(PA1, PA1), "Occupation" = c("PO - react to DCS, initiate corrective measures,  react to changes
", "PO - data based operations"), "Field" = c("PA","PA"), "Work" = c(0.5, 0.1), "Skill1" = c(CRO, CRO), "Skill2" = c(0, PPto), "ds" = c(5, 5))
print(df)

and the output should look like this

df_final = data.frame("ID" = c(PA1), "Occupation" = c("PO - react to DCS, initiate corrective measures,  react to changes, data based operations"), "Field" = c("PA"), "Work" = c(0.6), "Skill1" = c(CRO), "Skill2" = c(PPto), "ds" = c(5))
print(df_final)

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝不放开 2025-01-29 01:45:42

让我们暂时忽略Skill2:
以下代码与您想做什么有多近?

df2 %>%
  group_by(ID)%>%
  summarise(work = sum(Work), 
            skill1 = unique(Skill1),
            ds = unique(ds), 
            occupation = paste0(Occupation, collapse = " "),
            field = unique(Field))

您还可以突变(职业= str_replace_all(职业,“ po -')),以摆脱重复的“ po - ”。

如果Skill1/Skill2/ds之类的变量不是每个ID唯一的,那么您将遇到问题,例如它们具有基数> 1。

df2 %>%
  group_by(ID)%>%
  summarise(work = sum(Work), 
            skill1 = unique(Skill1),
            skill2 = unique(Skill2),
            ds = unique(ds), 
            occupation = paste0(Occupation, collapse = " "),
            field = unique(Field))

如果这是一个简单的数据输入问题,您可以进行一些纠纷以过滤仅包含字母的Skill2条目,然后将此框架与原始框架相结合。

您也可以使用past0()collapse = trick,但是您最终会得到Skill2 = C(na,“ ppto”),我很确定您不想要。

Let's ignore Skill2 for now:
How close is the following code to what you want to do?

df2 %>%
  group_by(ID)%>%
  summarise(work = sum(Work), 
            skill1 = unique(Skill1),
            ds = unique(ds), 
            occupation = paste0(Occupation, collapse = " "),
            field = unique(Field))

You can also mutate(occupation = str_replace_all(occupation, "PO - ")) to get rid of the duplicate "PO - "'s.

You're going to run into problems if the variables like Skill1/Skill2/ds are not unique to each ID, as in they have cardinality > 1.

df2 %>%
  group_by(ID)%>%
  summarise(work = sum(Work), 
            skill1 = unique(Skill1),
            skill2 = unique(Skill2),
            ds = unique(ds), 
            occupation = paste0(Occupation, collapse = " "),
            field = unique(Field))

If it's a simple data-entry issue, you could do a bit of wrangling to filter for only Skill2 entries with letters contained, and then join this frame back to your original frame.

You could also use the past0() collapse = trick, but then you'll end up with Skill2 = c(NA, "PPto"), which I'm pretty sure you don't want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文