gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:08:50 26 4
gpt4 key购买 nike

我有一个数据框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

我希望输出如下所示,其中缺少的信息需要为空格或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

感谢任何帮助。比q

最佳答案

使用末尾注释中可重复显示的数据,我们可以将 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)

关于r - 将数据框中的不均匀列拆分为 R 中的多个列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54947017/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com