英文:
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 的程序区域放在同一行上。我对以下两种方式都可以接受,要么像这样:
要么像这样:
无论哪种方式都可以。任何建议将不胜感激!
英文:
This is probably answered elsewhere but I cant figure out the phrasing to look for.
I have data like this:
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")
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:
or like this:
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论