【问题标题】:How to rewrite my dataframe transformation with data.frame如何用 data.frame 重写我的数据框转换
【发布时间】:2020-11-13 11:39:09
【问题描述】:

我有一个数据框:

ID       value
1      he following object is masked from ‘package:purrr’. R is free software and comes with ABSOLUTELY NO WARRANTY
2      Attaching package: ‘magrittr’. Natural language support but running in an English locale
2      Attaching package: ‘DT’. Natural language support but running in an English locale
2      Attaching package: ‘anytime’. Natural language support but running in an English locale
3      package ‘ggplot2’ was built under R version 3.6.2. Type 'contributors()' for more information
4      Warning messages: type 'demo()' for some demos, 'help()' for on-line help
4      Warning messages: 'help.start()' for an HTML browser interface to help

如何创建:

ID <- c(1,2,2,2,3,4,4)
value <- c("he following object is masked from ‘package:purrr’. R is free software and comes with ABSOLUTELY NO WARRANTY",
           "Attaching package: ‘magrittr’. Natural language support but running in an English locale",
           "Attaching package: ‘DT’. Natural language support but running in an English locale",
           "Attaching package: ‘anytime’. Natural language support but running in an English locale",
           "package ‘ggplot2’ was built under R version 3.6.2. Type 'contributors()' for more information",
           "Warning messages:type 'demo()' for some demos, 'help()' for on-line help",
           "Warning messages:'help.start()' for an HTML browser interface to help")



df <- data.table(ID, value)

我用这段代码转换它:

df_patterns <- df  %>% 
  mutate(pattern= stringr::str_extract(value, "\\S+\\s+\\S+\\s+\\S+"),
         pattern = coalesce(stringr::str_extract(pattern, "^Attaching package:|Warning messages:"),pattern),
         id_type = case_when(ID %in% c(1, 5) ~ "extra_type")
  ) %>%  
  group_by(ID, pattern) %>%
  summarise(example = sample(value,1)) %>%
  ungroup() %>%
  mutate(pattern=coalesce(pattern, example))

输出是:

ID            pattern                  example                                                                                                               id_type                               
1      he following object        he following object is masked from ‘package:purrr’. R is free software and comes with ABSOLUTELY NO WARRANTY             extra_type

2      Attaching package:         Attaching package: ‘anytime’. Natural language support but running in an English locale                                     NA

3      package ‘ggplot2’ was      package ‘ggplot2’ was built under R version 3.6.2. Type 'contributors()' for more information                               NA

4      Warning messages:          Warning messages:'help.start()' for an HTML browser interface to help                                                       NA

及其所需的输出。如您所见,我创建了新的列模式并按其分组数据表。我还添加了带有模式示例的列示例。

如何用 data.table 重写这个转换?我不想使用 mutate 和其他功能,而是想使用 data.table 的功能。但我不擅长它。我试过这个,但我不知道下一步该怎么做:

df_patterns <- df[, c("pattern", "id_type") := list(
  pattern = coalesce(stringr::str_extract(pattern= stringr::str_extract(value, "\\S+\\s+\\S+\\s+\\S+"), "^Attaching package:|Warning messages:"),pattern= stringr::str_extract(value, "\\S+\\s+\\S+\\s+\\S+")),
  case_when(ID %in% c(1, 5) ~ "extra_type")), by = ID, pattern]

【问题讨论】:

  • 您能解释一下预期的输出吗?
  • @zx8754 我添加了一些描述

标签: r dataframe data.table


【解决方案1】:

删除除data.table 之外的所有依赖项,以下内容应与您的预期输出匹配(但当然会在没有设置种子的情况下有所不同):

df_patterns <- 
  copy(df)[, pattern := fcase(
               startsWith(value, "Attaching package:"), "Attaching package:",
               startsWith(value, "Warning messages:"), "Warning messages:",
               rep(TRUE, nrow(df)), sub("((\\S+\\s+){2}\\S+).+", "\\1", value)
             )][, 
                .(
                  example = sample(value, 1), 
                  id_type = fifelse(ID %in% c(1,5),  "extra_type", NA_character_)
                ), 
                by = .(ID, pattern)]


   ID               pattern                                                                                                      example    id_type
1:  1   he following object he following object is masked from ‘package:purrr’. R is free software and comes with ABSOLUTELY NO WARRANTY extra_type
2:  2    Attaching package:                           Attaching package: ‘DT’. Natural language support but running in an English locale       <NA>
3:  3 package ‘ggplot2’ was                package ‘ggplot2’ was built under R version 3.6.2. Type 'contributors()' for more information       <NA>
4:  4     Warning messages:                                     Warning messages:type 'demo()' for some demos, 'help()' for on-line help       <NA>

【讨论】:

  • 找不到函数“fcase”。我在 data.table 包中没有那个功能
  • 我将列 id_type 添加到所需的结果中。我一开始就忘记了
  • @french_fries 你有一个旧版本的data.table。要安装最新版本,请使用install.packages('data.table')。更新了解决方案以包含 id_type
猜你喜欢
  • 2011-01-05
  • 2019-09-09
  • 1970-01-01
  • 1970-01-01
  • 2019-02-12
  • 1970-01-01
  • 2021-12-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多