【问题标题】:`separate()` function from tidyr splitting on space instead of pattern`separate()` 函数来自 tidyr 在空间而不是模式上拆分
【发布时间】:2019-04-20 17:49:19
【问题描述】:

从包含州和县信息的字符串中,我想分成两列,分别来自州和县。使用函数separate,甚至使用模式",",或"[,]""[\\,]""\\," 等变体,但我继续在空格上分割字符串,而不是模式。

查看示例

data.frame(test = c("montana,sheridan",
                    "north dakota,divide",
                    "new york,clinton")) %>%
  separate(test, c("state", "county"), by=",", extra = "drop")

【问题讨论】:

  • 参数是sep 而不是by。检查?separate,Usage 是separate(data, col, into, sep = "[^[:alnum:]]+", remove = TRUE, convert = FALSE, extra = "warn", fill = "warn", ...)
  • 是的...我尝试使用更复杂的解决方案,但这是一个非常基本的问题。谢谢

标签: r tidyr separator


【解决方案1】:

我认为您设置了错误的参数。使用sep = "," 而不是by = ","

test = c("montana,sheridan",
     "north dakota,divide",
     "new york,clinton")
data.frame(test) %>%
     separate(test, c("state", "county"),
     sep=",", extra = "drop")

这对我有用。希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 2019-08-12
    • 2022-12-15
    • 2017-07-16
    • 2015-11-14
    • 1970-01-01
    • 1970-01-01
    • 2021-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多