将重复项放在同一行,R

huangapple go评论72阅读模式
英文:

Put duplicates in same row, R

问题

以下是翻译好的内容:

这个问题可能已经在其他地方有答案,但我无法找到正确的措辞来搜索。

我有这样的数据:

df <- structure(list(PROTOCOL_ID = c(124, 124, 38, 762, 74, 146), PROGRAM_AREA = c("LOCR", "CRC", "LOCR", "Pedi", "LOCR", "LOCR")), row.names = c(NA, 6L), class = "data.frame")

正如您所见,两行具有相同的 "protocol_id",它们都是 124(在实际数据中有很多具有相同编号的行)。那么,我想要的是将 124 的程序区域放在同一行上。我对以下两种方式都可以接受,要么像这样:

将重复项放在同一行,R

要么像这样:

将重复项放在同一行,R

无论哪种方式都可以。任何建议将不胜感激!

英文:

This is probably answered elsewhere but I cant figure out the phrasing to look for.

I have data like this:

df&lt;-structure(list(PROTOCOL_ID = c(124, 124, 38, 762, 74, 146), PROGRAM_AREA = c(&quot;LOCR&quot;, 
&quot;CRC&quot;, &quot;LOCR&quot;, &quot;Pedi&quot;, &quot;LOCR&quot;, &quot;LOCR&quot;)), row.names = c(NA, 6L
), class = &quot;data.frame&quot;)

As you can see, two rows have the same "protocol_id", they're both 124 (lots of rows in the real data have identical numbers). Well, what I'd like to do is put the program areas for 124 on the same row. I'm ok with this being one of two ways, either like this:

将重复项放在同一行,R

or like this:

将重复项放在同一行,R

Whatever is easiest. Any suggestions would be appreciated!

答案1

得分: 0

我们可以使用

library(dplyr) # 版本 >= 1.1.0
df %>%
reframe(PROGRAM_AREA = toString(PROGRAM_AREA), .by = PROTOCOL_ID)
PROTOCOL_ID PROGRAM_AREA
1 124 LOCR, CRC
2 38 LOCR
3 762 Pedi
4 74 LOCR
5 146 LOCR


---

或者对于第二种情况

library(tidyr)
library(data.table)
library(english)
df %>%
mutate(rn = toupper(as.character(english(rowid(PROTOCOL_ID))))) %>%
pivot_wider(names_from = rn, values_from = PROGRAM_AREA,
names_prefix = "PROGRAM_AREA_")

一个数据框: 5 × 3

PROTOCOL_ID PROGRAM_AREA_ONE PROGRAM_AREA_TWO

1 124 LOCR CRC
2 38 LOCR NA
3 762 Pedi NA
4 74 LOCR NA
5 146 LOCR NA


<details>
<summary>英文:</summary>

We could use

library(dplyr) # version >= 1.1.0
df %>%
reframe(PROGRAM_AREA = toString(PROGRAM_AREA), .by = PROTOCOL_ID)
PROTOCOL_ID PROGRAM_AREA
1 124 LOCR, CRC
2 38 LOCR
3 762 Pedi
4 74 LOCR
5 146 LOCR


---

Or for the second case

library(tidyr)
library(data.table)
library(english)
df %>%
mutate(rn = toupper(as.character(english(rowid(PROTOCOL_ID))))) %>%
pivot_wider(names_from = rn, values_from = PROGRAM_AREA,
names_prefix = "PROGRAM_AREA_")

A tibble: 5 × 3

PROTOCOL_ID PROGRAM_AREA_ONE PROGRAM_AREA_TWO
<dbl> <chr> <chr>
1 124 LOCR CRC
2 38 LOCR <NA>
3 762 Pedi <NA>
4 74 LOCR <NA>
5 146 LOCR <NA>


</details>



huangapple
  • 本文由 发表于 2023年3月7日 03:17:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75654945.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定