【问题标题】:Splitting column into two in R在R中将列拆分为两个
【发布时间】:2019-05-02 03:07:25
【问题描述】:

我现在在 R 中使用这个当前数据框,我的目标是使用 tidyr 中的单独函数将 song_genre 列分成两列:

songs <- c("Wheel in the Sky", "Smooth Criminal", "Bloodstream", "New Kid in 
Town", "You Belong with Me")
length <- c(211, 209, 299, 304, 232)
genre <- c("Rock", "Pop", "Pop", "Classic Rock", "Country Pop")
songList <- data.frame(songs, length, genre)
songList
songUnite <- unite(songList, "songs_genre", c("songs", "genre"), sep=".")
songUnite

但是,当我在命令中输入分隔符时:

songSeparate <- separate(songUnite, col = songs_genre, into = c("songs", "genre"), sep=".")
songSeparate

出现此警告:

警告信息:预计 2 件。额外的碎片被丢弃在 5 行 [1, 2, 3, 4, 5] 中。`

我已经在网上检查了我的格式和变量是否都在正确的位置,但似乎无法在我所写的内容中找到错误。

我还包含库 (tidyr)

【问题讨论】:

    标签: r tidyr


    【解决方案1】:

    你已经用sep = "\\."“逃脱”了.

    . 是一个特殊的正则表达式字符,它匹配任何字符,除非转义。最好使用_ 等分隔符来避免此问题。

    【讨论】:

    • 我遇到了类似的问题,在搜索时发现了这个问题——我使用了分隔符“。”和“|”但是当我试图分开时,我将每个数字都作为一列。使用“_”作为分隔符解决了我的问题。感谢您的建议。
    【解决方案2】:

    您还可以使用包stringr 拆分列:

    require(stringr)
    
    # data:
    twowords <- c("hi there", "there how", "how are", "are you")
    
    ### split into two columns:
    dat <- data.frame(
      word1 = str_extract(twowords, "\\w.*(?=\\s)"), # regex says: match if you see space on the right
      word2 = str_extract(twowords, "(?<=\\s)\\w.*") # regex says: match if you see space on the left
       )
    dat
      word1 word2
    1    hi there
    2 there   how
    3   how   are
    4   are   you
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-05
      • 2019-07-06
      • 1970-01-01
      • 2020-03-26
      • 2013-08-09
      • 2017-12-16
      • 2021-12-28
      • 2019-04-05
      相关资源
      最近更新 更多