R 旋转单列以将数据放入一行
R Pivot single column to place data into a line
我想对一列进行旋转,这样我就有了一行数据。
目前我有两列,希望第二列放在一行上
数据:
structure(list(Specimen.Number.Full = structure(c(73L, 73L, 1L,
1L, 1L, 1L, 1L, 3L, 3L, 3L), .Label = c("CN000007S/22", "CN000009Z/22",
"CN000014B/22", "CN000015R/22", "CN000016X/22", "CN000026S/22",
"CN000030K/22", "CN000047X/22", "CN000049L/22", "CN000050A/22",
"CN000088T/22", "CN000090Z/22", "CN000091N/22", "CN000116M/22",
"CN000117J/22", "CN000185E/22", "CN000186H/22", "CN000202V/22",
"CN000203B/22", "CN000204R/22", "CN000205X/22", "CN000224X/22",
"CN000225D/22", "CN000226L/22", "CN000305M/22", "CN000322T/22",
"CN000326V/22", "CN000327B/22", "CN000331P/22", "CN000349J/22",
"CN000390R/22", "CN000398E/22", "CN000399H/22", "CN000402Y/22",
"CN000456H/22", "CN000469Y/22", "CN000484D/22", "CN000506H/22",
"CN000523Q/22", "CN000525H/22", "CN000557Z/22", "CN000568G/22",
"CN000570T/22", "CN000586B/22", "CN000606G/22", "CN000607Z/22",
"CN000608N/22", "CN000610M/22", "CN000620B/22", "CN000621R/22",
"CN000624S/22", "CN000625G/22", "CN000626Z/22", "CN000631V/22",
"CN000644R/22", "CN000645X/22", "CN000646D/22", "CN000647L/22",
"CN000648S/22", "CN000655B/22", "CN000656R/22", "CN000675K/22",
"CN000681Y/22", "CN000682P/22", "CN000683A/22", "CN000700C/22",
"CN000726X/22", "CN000756K/22", "CN000757M/22", "CN000758J/22",
"CN000759V/22", "CN000798W/22", "PR000048H/22", "PR000134L/22",
"PR000259Q/22", "PR000407X/22", "PR000599Q/22", "PR000905J/22",
"PR000956C/22", "PR000978A/22", "PR001017D/22", "PR001110W/22",
"PR001306J/22", "PR001633X/22", "PR001715J/22", "PR001776A/22",
"PR001844P/22", "PR001995B/22", "PR002001D/22", "PR002054X/22",
"PR002497Q/22", "PR002608W/22", "PR003018R/22", "PR003194L/22",
"PR003265A/22", "PR003294N/22", "PR003377E/22", "PR003524Z/22",
"PR003707V/22", "PR004375B/22", "PR004637T/22", "PR004769Q/22",
"PR004822S/22", "PR005090H/22", "PR005105X/22", "PR005133V/22",
"PR005698V/22", "PR005758X/22", "PR006170X/22", "PR006193X/22",
"PR006600T/22", "PR006626D/22", "PR006905Q/22", "PR007427G/22",
"PR007962P/22", "PR008110F/22", "PR008449K/22", "PR008534H/22",
"PR008537P/22", "PR008607E/22", "PR008748D/22", "PR008826B/22",
"PR008928M/22", "PR009127K/22", "PR009298E/22", "PR009489D/22",
"PR009590Q/22", "PR009596F/22", "PR009666C/22", "PR009668K/22",
"PR010000C/22", "PR010027D/22", "PR010055T/22", "PR010145A/22",
"PR010511B/22", "PR010940R/22", "PR011196J/22", "PR011482W/22",
"PR011557K/22", "PR011601Y/22", "PR011882F/22", "PR011992Q/22",
"PR012389Z/22", "PR012695Q/22", "PR780367W/22"), class = "factor"),
Secondary.Code = structure(c(14L, 49L, 29L, 33L, 42L, 25L,
34L, 29L, 33L, 41L), .Label = c("M00100", "M09000", "M09350",
"M14110", "M42100", "M64300", "M73320", "M74000", "M80103",
"M80106", "M80413", "M80703", "M81402", "M81403", "M81406",
"M82100", "M82103", "M82110", "M82630", "M82900", "M84723",
"M84803", "M87203", "MM0017", "MM0018", "P11431", "P1149",
"P30851", "P30852", "P30853", "P30855", "P30866", "P30871",
"P30890", "P30891", "P30892", "P30893", "P30894", "P30895",
"P30896", "P30897", "P30898", "P31000", "P97002", "P97003",
"P97004", "P97005", "P97006", "P97008", "P97019", "P97020"
), class = "factor")), row.names = c(1L, 3L, 4L, 5L, 6L,
7L, 8L, 14L, 15L, 16L), class = "data.frame")
我有R代码:
MSI.Secondary %>%
pivot_wider(names_from = Secondary.Code ,values_from = Secondary.Code )
期望输出:
| Specimen.Number.Full | Secondary.Code |
| ------------------ | -------------- |
| PR011196J/22 | M81406|
| PR000048H/22 | M81403| P97008 |
我得到的输出:
| Specimen.Number.Full | Secondary.Code |
| ------------------ | -------------- |
| PR011196J/22 |不适用 |不适用 |不适用 |不适用 |不适用 |M81406|
| PR000048H/22 | M81403| P97008 |不适用 |不适用 |不适用 |不适用 |
使用 group_by
+ summarise
你可以:
library(dplyr)
MSI.Secondary |>
group_by(Specimen.Number.Full) |>
summarise(Secondary.Code = paste(Secondary.Code, collapse = ", "))
#> # A tibble: 3 × 2
#> Specimen.Number.Full Secondary.Code
#> <fct> <chr>
#> 1 CN000007S/22 P30852, P30871, P30898, MM0018, P30890
#> 2 CN000014B/22 P30852, P30871, P30897
#> 3 PR000048H/22 M81403, P97008
我想对一列进行旋转,这样我就有了一行数据。 目前我有两列,希望第二列放在一行上
数据:
structure(list(Specimen.Number.Full = structure(c(73L, 73L, 1L,
1L, 1L, 1L, 1L, 3L, 3L, 3L), .Label = c("CN000007S/22", "CN000009Z/22",
"CN000014B/22", "CN000015R/22", "CN000016X/22", "CN000026S/22",
"CN000030K/22", "CN000047X/22", "CN000049L/22", "CN000050A/22",
"CN000088T/22", "CN000090Z/22", "CN000091N/22", "CN000116M/22",
"CN000117J/22", "CN000185E/22", "CN000186H/22", "CN000202V/22",
"CN000203B/22", "CN000204R/22", "CN000205X/22", "CN000224X/22",
"CN000225D/22", "CN000226L/22", "CN000305M/22", "CN000322T/22",
"CN000326V/22", "CN000327B/22", "CN000331P/22", "CN000349J/22",
"CN000390R/22", "CN000398E/22", "CN000399H/22", "CN000402Y/22",
"CN000456H/22", "CN000469Y/22", "CN000484D/22", "CN000506H/22",
"CN000523Q/22", "CN000525H/22", "CN000557Z/22", "CN000568G/22",
"CN000570T/22", "CN000586B/22", "CN000606G/22", "CN000607Z/22",
"CN000608N/22", "CN000610M/22", "CN000620B/22", "CN000621R/22",
"CN000624S/22", "CN000625G/22", "CN000626Z/22", "CN000631V/22",
"CN000644R/22", "CN000645X/22", "CN000646D/22", "CN000647L/22",
"CN000648S/22", "CN000655B/22", "CN000656R/22", "CN000675K/22",
"CN000681Y/22", "CN000682P/22", "CN000683A/22", "CN000700C/22",
"CN000726X/22", "CN000756K/22", "CN000757M/22", "CN000758J/22",
"CN000759V/22", "CN000798W/22", "PR000048H/22", "PR000134L/22",
"PR000259Q/22", "PR000407X/22", "PR000599Q/22", "PR000905J/22",
"PR000956C/22", "PR000978A/22", "PR001017D/22", "PR001110W/22",
"PR001306J/22", "PR001633X/22", "PR001715J/22", "PR001776A/22",
"PR001844P/22", "PR001995B/22", "PR002001D/22", "PR002054X/22",
"PR002497Q/22", "PR002608W/22", "PR003018R/22", "PR003194L/22",
"PR003265A/22", "PR003294N/22", "PR003377E/22", "PR003524Z/22",
"PR003707V/22", "PR004375B/22", "PR004637T/22", "PR004769Q/22",
"PR004822S/22", "PR005090H/22", "PR005105X/22", "PR005133V/22",
"PR005698V/22", "PR005758X/22", "PR006170X/22", "PR006193X/22",
"PR006600T/22", "PR006626D/22", "PR006905Q/22", "PR007427G/22",
"PR007962P/22", "PR008110F/22", "PR008449K/22", "PR008534H/22",
"PR008537P/22", "PR008607E/22", "PR008748D/22", "PR008826B/22",
"PR008928M/22", "PR009127K/22", "PR009298E/22", "PR009489D/22",
"PR009590Q/22", "PR009596F/22", "PR009666C/22", "PR009668K/22",
"PR010000C/22", "PR010027D/22", "PR010055T/22", "PR010145A/22",
"PR010511B/22", "PR010940R/22", "PR011196J/22", "PR011482W/22",
"PR011557K/22", "PR011601Y/22", "PR011882F/22", "PR011992Q/22",
"PR012389Z/22", "PR012695Q/22", "PR780367W/22"), class = "factor"),
Secondary.Code = structure(c(14L, 49L, 29L, 33L, 42L, 25L,
34L, 29L, 33L, 41L), .Label = c("M00100", "M09000", "M09350",
"M14110", "M42100", "M64300", "M73320", "M74000", "M80103",
"M80106", "M80413", "M80703", "M81402", "M81403", "M81406",
"M82100", "M82103", "M82110", "M82630", "M82900", "M84723",
"M84803", "M87203", "MM0017", "MM0018", "P11431", "P1149",
"P30851", "P30852", "P30853", "P30855", "P30866", "P30871",
"P30890", "P30891", "P30892", "P30893", "P30894", "P30895",
"P30896", "P30897", "P30898", "P31000", "P97002", "P97003",
"P97004", "P97005", "P97006", "P97008", "P97019", "P97020"
), class = "factor")), row.names = c(1L, 3L, 4L, 5L, 6L,
7L, 8L, 14L, 15L, 16L), class = "data.frame")
我有R代码:
MSI.Secondary %>%
pivot_wider(names_from = Secondary.Code ,values_from = Secondary.Code )
期望输出:
| Specimen.Number.Full | Secondary.Code |
| ------------------ | -------------- |
| PR011196J/22 | M81406|
| PR000048H/22 | M81403| P97008 |
我得到的输出:
| Specimen.Number.Full | Secondary.Code |
| ------------------ | -------------- |
| PR011196J/22 |不适用 |不适用 |不适用 |不适用 |不适用 |M81406|
| PR000048H/22 | M81403| P97008 |不适用 |不适用 |不适用 |不适用 |
使用 group_by
+ summarise
你可以:
library(dplyr)
MSI.Secondary |>
group_by(Specimen.Number.Full) |>
summarise(Secondary.Code = paste(Secondary.Code, collapse = ", "))
#> # A tibble: 3 × 2
#> Specimen.Number.Full Secondary.Code
#> <fct> <chr>
#> 1 CN000007S/22 P30852, P30871, P30898, MM0018, P30890
#> 2 CN000014B/22 P30852, P30871, P30897
#> 3 PR000048H/22 M81403, P97008