将数据框中的不均匀列拆分为R中的多个列

时间:2019-03-01 14:50:35

标签: r split dplyr tidyr

我有一个数据框A,如下所示,其中列Info缺少一些信息,例如Sample2没有其他颜色,白色或黑色:

可复制的示例:

A <- structure(list(Sample = structure(1:7, .Label = c("Sample1", 
"Sample2", "Sample3", "Sample4", "Sample5", "Sample6", "Sample7"
), class = "factor"), Description = structure(c(7L, 3L, 4L, 2L, 
6L, 1L, 5L), .Label = c("37 years, female, white, alive, 257 days", 
"43 years, male, white, stage:iiic, alive, 598 days", "53 years, male, stage:iiib, alive, 792 days", 
"68 years, female, white, stage:iiic, dead, 740 days", "69 years, female, black or african american, stage:iia, alive, 627 days", 
"74 years, white, stage:i, alive, 1001 days", "82 years, female, white, stage:iiib, alive, 1419 days"
), class = "factor")), class = "data.frame", row.names = c(NA, 
-7L))

数据帧A如下所示:

Sample  Info
Sample1 82 years, female, white, stage:iiib, alive, 1419 days
Sample2 53 years, male, stage:iiib, alive, 792 days
Sample3 68 years, female, white, stage:iiic, dead, 740 days
Sample4 43 years, male, white, stage:iiic, alive, 598 days
Sample5 74 years, white, stage:i, alive, 1001 days
Sample6 37 years, female, white, alive, 257 days
Sample7 69 years, female, black, stage:iia, alive, 627 days

要将Info列分成多个列,我使用了如下的separate函数

library(dplyr)
library(tidyr)
A2 <- separate(A, 'Info', paste("Info", 1:6, sep="_"), sep=",", extra="drop")

但是新列看起来像下面这样不均匀:

Sample  Info_1     Info_2   Info_3    Info_4    Info_5    Info_6
Sample1 82 years   female    white   stage:iiib  alive   1419 days
Sample2 53 years    male  stage:iiib    alive   792 days    NA
Sample3 68 years   female    white   stage:iiic  dead    740 days
Sample4 43 years    male     white   stage:iiic  alive   598 days
Sample5 74 years   white    stage:i    alive     1001 days  NA
Sample6 37 years   female    white     alive     257 days   NA
Sample7 69 years   female    black   stage:iia   alive   627 days

我希望output如下所示,其中缺少的信息需要为空白或NA,最后一列仅显示其中没有任何单词days的数字:

Sample  Info_1     Info_2   Info_3    Info_4    Info_5   Info_6
Sample1 82 years   female    white   stage:iiib  alive   1419
Sample2 53 years    male             stage:iiib  alive   792    
Sample3 68 years   female    white   stage:iiic  dead    740
Sample4 43 years    male     white   stage:iiic  alive   598
Sample5 74 years             white    stage:i    alive   1001
Sample6 37 years   female    white               alive   257
Sample7 69 years   female    black   stage:iia   alive   627

感谢您的帮助。谢谢

2 个答案:

答案 0 :(得分:3)

使用结尾处“注释”中可重复显示的数据,我们可以将read.pattern与指示的模式pat一起使用,然后删除垃圾列(其他所有列)。如果您不要求列名与问题中的名称完全相同,则可以省略标记为##的行。

library(gsubfn)

pat <- 
"((\\d+ years), )?((female|male), )?((white|black), )?((stage:\\S+), )?((alive|dead), )?((\\d+) days)?"
r <- read.pattern(text = as.character(DF$Info), pattern = pat, as.is = TRUE)
DF2 <- cbind(Sample = DF$Sample, r[c(FALSE, TRUE)], stringsAsFactors = FALSE)

nc <- ncol(DF2) ## 
names(DF2)[-1] <- paste0("Info_", 1:(nc-1)) ##

DF2

给予:

   Sample   Info_1 Info_2 Info_3     Info_4 Info_5 Info_6
1 Sample1 82 years female  white stage:iiib  alive   1419
2 Sample2 53 years   male        stage:iiib  alive    792
3 Sample3 68 years female  white stage:iiic   dead    740
4 Sample4 43 years   male  white stage:iiic  alive    598
5 Sample5 74 years         white    stage:i  alive   1001
6 Sample6 37 years female  white             alive    257
7 Sample7 69 years female  black  stage:iia  alive    627

注意

可重复形式的输入DF如下。

Lines <- "
Sample;Info
Sample1;82 years, female, white, stage:iiib, alive, 1419 days
Sample2;53 years, male, stage:iiib, alive, 792 days
Sample3;68 years, female, white, stage:iiic, dead, 740 days
Sample4;43 years, male, white, stage:iiic, alive, 598 days
Sample5;74 years, white, stage:i, alive, 1001 days
Sample6;37 years, female, white, alive, 257 days
Sample7;69 years, female, black, stage:iia, alive, 627 days"

DF <- read.table(text = Lines, header = TRUE, sep = ";", as.is = TRUE, strip.white = TRUE)

答案 1 :(得分:2)

这是一个主意。我相信有更好的方法,但这是一个开始。

我们可以使用int *中的printf("%d",*b); 来拆分从完整记录开始的列。关键是设置可识别且有意义的列名。之后,我们用*b删除行,并将其从原始数据框中删除。然后我们假设某些列丢失了,可以再次执行int。最终,我们可以满足所有缺失的条件并适当地将它们分开。最后一步是合并所有子集数据帧。

如果缺少许多不同的列组件,则此方法可能太麻烦了。但是,如果您确定可能会丢失什么。我们可以设计一个函数并将所有这些步骤包装在函数中。

extract

数据

tidyr