【问题标题】:How to use separate in tidyverse to split a column?如何在tidyverse中使用separate来拆分列?
【发布时间】:2021-10-17 00:42:57
【问题描述】:

我使用separate()函数拆分列:Enterdateofexam2,它是字符格式,值如“25.07”,“13.09”,“16.06”...我的目标是将它拆分为日(25)和月(07),然后使用 convert = true 将它们转换为数字进行下一步过滤。

我的代码是:

jimma3n <- jimma3 %>%
        select(Enterdateofexam2, Enterdayofexam, UniqueKey,MEDICALRECORD)%>%
        separate(Enterdateofexam2,into=c("day", "month"), sep=".", convert = TRUE)
view (jimma3n)

但 R 一直在警告我:

Expected 2 pieces. Additional pieces discarded in 4088 rows [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, ...].

那么任何人都可以帮助找出我的代码的哪一部分有问题吗?谢谢~~!

【问题讨论】:

  • 可以发一下dput(head(data))吗?
  • 我运行它,它显示:structure(list(Enterdateofexam2 = list(c("", "7.06"), c("", "8.06"), c("", "9.06 "), c("", "2.12"), c("", "3.12"), c("", "4.12")), Enterdayofexam = c("1", "2", "3", "1", "2", "3"), UniqueKey = c("530", "530", "530", "531", "531", "531"), MEDICALRECORD = c("577207", “577207”、“577207”、“575333”、“575333”、“575333”)),row.names = c(NA,-6L),class= c(“tbl_df”,“tbl”,“data.frame ")),为什么数字前面有("",...),如何去掉"",只保留列中的值?非常感谢。
  • 这是因为您的Enterdateofexam2 已作为列表加载到您的data.frame 中,这就是您收到错误的原因!

标签: r dplyr tidyverse tidyr


【解决方案1】:

我们可以使用extra 参数。此外,默认情况下,sep 处于regex 模式 - 根据?separate 文档

sep - 如果是字符,则 sep 被解释为正则表达式。默认值是匹配任何非字母数字值序列的正则表达式。

. 是一个可以匹配任何字符的元字符。因此,我们可能需要转义 (\\.) 或将其放在方括号中 ([.])。另外,基于dput,列是list,应该先unnested,然后再做separate

library(dplyr)
library(tidyr)
jimma3 %>%
      select(Enterdateofexam2, Enterdayofexam, UniqueKey,MEDICALRECORD)%>%
      unnest(Enterdateofexam2) %>%
      separate(Enterdateofexam2,into=c("day", "month"), 
              sep="\\.", convert = TRUE, extra = "merge") %>% 
      na.omit

-输出

# A tibble: 6 x 5
    day month Enterdayofexam UniqueKey MEDICALRECORD
  <int> <int> <chr>          <chr>     <chr>        
1     7     6 1              530       577207       
2     8     6 2              530       577207       
3     9     6 3              530       577207       
4     2    12 1              531       575333       
5     3    12 2              531       575333       
6     4    12 3              531       575333       

基本上,使用sep = ".",它会在每个字符元素处分裂,因此会弹出警告

数据

jimma3 <- structure(list(Enterdateofexam2 = list(c("", "7.06"), c("", "8.06"
), c("", "9.06"), c("", "2.12"), c("", "3.12"), c("", "4.12")), 
    Enterdayofexam = c("1", "2", "3", "1", "2", "3"), UniqueKey = c("530", 
    "530", "530", "531", "531", "531"), MEDICALRECORD = c("577207", 
    "577207", "577207", "575333", "575333", "575333")), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))

【讨论】:

    【解决方案2】:

    主要问题是您必须定义要分开的列数。如果您定义 2 列,假设 ab 并且您有 3 个元素要分开:假设 x y z,那么 z 将被丢弃。

    对于separate,您必须定义新列,如果您不知道在separate 之后需要多少列,这很困难

    考虑这个例子: 在第 3 行中,您有 3 个元素:

    df <- data.frame(x = c("x", "x y", "x y z", NA))
          x
    1     x
    2   x y
    3 x y z
    4  <NA>
    

    使用此代码,您可以定义 2 列来分隔

    df %>% separate(x, c("a", "b"))
    
         a    b
    1    x <NA>
    2    x    y
    3    x    y
    4 <NA> <NA>
    

    在第 3 行中,z 被丢弃,因为我们只定义了 2 列 ab

    如果我们定义 3 列像

    df %&gt;% separate(x, c("a", "b", "c"))

    丢弃警告将消失。

    另一方面,如果 x 元素少于 3 个,则会收到警告,这些元素将被 NA 填充。

    【讨论】:

      猜你喜欢
      • 2019-08-12
      • 2020-10-05
      • 2019-06-07
      • 1970-01-01
      • 1970-01-01
      • 2016-08-03
      • 2015-05-11
      • 2021-06-22
      • 2017-12-30
      相关资源
      最近更新 更多