【问题标题】:Split an uneven column in a dataframe into multiple columns in R将数据框中的不均匀列拆分为 R 中的多列
【发布时间】:2019-07-23 15:03:15
【问题描述】:

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

感谢任何帮助。比q

【问题讨论】:

  • 似乎需要一些逻辑来将值与您尚未编程的正确列进行匹配,即 Info_3 包含信息“white”或“black”,Info_4 包含字符串“stage:”后跟几个字母等。separate 无法为您解决这个问题,它只会用一些分隔符分割字符串
  • 最简单的解决方案是转到源并以不同的方式导出数据,以便包含空单元格,无论是没有信息还是“NA”。所以如果你有 A 和 C 但没有 B,你会看到 "(...), A, , C, (...)"。
  • @Sotos 我已经在这里举了一个例子。我还展示了我尝试了什么,以及我需要的输出应该是什么样子。
  • @user3351523 看看我给你的链接。您需要发布可重现的示例(我可以轻松复制/粘贴到我的会话中)

标签: r split dplyr tidyr


【解决方案1】:

这是一个想法。我确信有更好的方法,但这是一个开始。

我们可以使用tidyr 中的extract 来拆分从完整记录开始的列。关键是设置可识别且有意义的列名。在此之后,我们删除带有NA 的行并将它们从原始数据框中删除。然后我们可以再次执行extract,假设缺少某些列。最终,我们可以满足所有缺失的条件并将它们正确分离。最后一步是合并所有子集数据帧。

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

library(tidyverse)

# Complete rows
dat2 <- dat %>%
  extract(Info, into = c("Year", "Sex", "Race", "Stage", "Status", "Days"),
          regex = "([0-9]* years), (male|female), (black|white), (stage\\:i[A-Za-z]*), (dead|alive), ([0-9]*)") %>%
  drop_na(Year)

dat <- dat %>% anti_join(dat2, by = "Sample")

# Record with no race
dat3 <- dat %>%
  extract(Info, into = c("Year", "Sex", "Stage", "Status", "Days"),
          regex = "([0-9]* years), (male|female), (stage\\:i[A-Za-z]*), (dead|alive), ([0-9]*)") %>%
  drop_na(Year)

dat <- dat %>% anti_join(dat3, by = "Sample")

# Record with no sex
dat4 <- dat %>%
  extract(Info, into = c("Year", "Race", "Stage", "Status", "Days"),
          regex = "([0-9]* years), (black|white), (stage\\:i[A-Za-z]*), (dead|alive), ([0-9]*)") %>%
  drop_na(Year)

dat <- dat %>% anti_join(dat4, by = "Sample")

# Record with no stage
dat5 <- dat %>%
  extract(Info, into = c("Year", "Sex", "Race", "Status", "Days"),
          regex = "([0-9]* years), (male|female), (black|white), (dead|alive), ([0-9]*)") %>%
  drop_na(Year)

dat <- dat %>% anti_join(dat5, by = "Sample")

# Combine all subset data frame
dat_new <- bind_rows(dat2, dat3, dat4, dat5) %>%
  arrange(Sample)
dat_new
#    Sample     Year    Sex  Race      Stage Status Days
# 1 Sample1 82 years female white stage:iiib  alive 1419
# 2 Sample2 53 years   male  <NA> 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   <NA> white    stage:i  alive 1001
# 6 Sample6 37 years female white       <NA>  alive  257
# 7 Sample7 69 years female black  stage:iia  alive  627

数据

dat <- read.table(text = "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'",
                  header = TRUE, stringsAsFactors = FALSE)

【讨论】:

  • 优秀。非常感谢。如果您不介意,正如我在所需输出中提到的,我不希望在最后一列中出现天数。
  • @user3351523 更新帖子以满足您的要求。谢谢。
【解决方案2】:

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

【讨论】:

  • 比我的方法更好,因为它是一种更灵活的方法。感谢分享。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-09
  • 2017-03-29
  • 2023-03-03
  • 1970-01-01
  • 2021-09-28
  • 1970-01-01
相关资源
最近更新 更多