我现在正在使用 R 中的当前数据框,我的目标是使用 tidyr 中的单独函数将 songs_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
出现此警告:
Warning message: Expected 2 pieces. Additional pieces discarded in 5 rows [1, 2, 3, 4, 5].`
我已经在线检查过我的格式和变量是否都在正确的位置,但似乎无法找到我所写内容的错误。
我还包含了库 (tidyr)
最佳答案
您已使用 sep = "\\."
对 .
进行了“转义”。
.
是一个特殊的正则表达式字符,它匹配任何字符,除非转义。最好使用 _
等分隔符来避免此问题。
关于r - 在R中将列一分为二,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53549419/