簡體   English   中英

將因子轉換為 R 中的多列

[英]Convert factors into multiple columns in R

我有一個包含 PatientID 和他們的疾病測試結果的數據集,它們如下:

Id  Result
1   Strep A: Positive
2   Flu A: Negative, Flu B: Negative
3   Rsv: Positive, RsvA: Negative, RsvB: Positive
4   Strep A: Negative
5   Flu A: Negative, Flu B: Negative
6   Flu A: Negative, Flu B: Negative
7   Strep A: Positive

如何拆分Result列如下:

Id  Result_Strep A  Result_Flu A    Result_Flu B    Result_Rsv  Result_RsvA Result_RsvB
1   Positive        NA              NA              NA          NA          NA
2   NA              Negative        Negative        NA          NA          NA
3   NA              NA              NA              Positive    Negative    Positive
4   Negative        NA              NA              NA          NA          NA
5   NA              Negative        Negative        NA          NA          NA
6   NA              Negative        Negative        NA          NA          NA
7   Positive        NA              NA              NA          NA          NA

數據輸出

structure(list(Id = 1:7, Result = c("Strep A: Positive", "Flu A: Negative, Flu B: Negative", 
"Rsv: Positive, RsvA: Negative, RsvB: Positive", "Strep A: Negative", 
"Flu A: Negative, Flu B: Negative", "Flu A: Negative, Flu B: Negative", 
"Strep A: Positive")), row.names = c(NA, -7L), class = "data.frame")

我們可以使用separate_rows在拆分, ,然后separate列一分為二,並重塑成“寬”格式

library(dplyr)
library(tidyr)
library(stringr)
df1 %>%
   separate_rows(Result, sep=", ") %>% 
   separate(Result, into = c("Result1", "Result2"), sep=":\\s*") %>% 
   mutate(Result1 = str_c("Result_", Result1)) %>%
   # in case of duplicate elements uncomment the commented code below
   #group_by(Result1) %>%       
   #mutate(rn = row_number()) %>% 
   #ungroup %>%
   pivot_wider(names_from = Result1, values_from = Result2)# %>%
   #select(-rn)
# A tibble: 7 x 7
#     Id `Result_Strep A` `Result_Flu A` `Result_Flu B` Result_Rsv Result_RsvA Result_RsvB
#  <int> <chr>            <chr>          <chr>          <chr>      <chr>       <chr>      
#1     1 Positive         <NA>           <NA>           <NA>       <NA>        <NA>       
#2     2 <NA>             Negative       Negative       <NA>       <NA>        <NA>       
#3     3 <NA>             <NA>           <NA>           Positive   Negative    Positive   
#4     4 Negative         <NA>           <NA>           <NA>       <NA>        <NA>       
#5     5 <NA>             Negative       Negative       <NA>       <NA>        <NA>       
#6     6 <NA>             Negative       Negative       <NA>       <NA>        <NA>       
#7     7 Positive         <NA>           <NA>           <NA>       <NA>        <NA>       

數據

df1 <- structure(list(Id = 1:7, Result = c("Strep A: Positive", 
 "Flu A: Negative, Flu B: Negative", 
"Rsv: Positive, RsvA: Negative, RsvB: Positive", "Strep A: Negative", 
"Flu A: Negative, Flu B: Negative", "Flu A: Negative, Flu B: Negative", 
"Strep A: Positive")), class = "data.frame", row.names = c(NA, 
-7L))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM