在R中将列一分为二
我现在正在R中使用当前的数据帧,我的目标是使用tidyr中的分离功能将songs_流派列分为两部分:在R中将列一分为二,r,tidyr,R,Tidyr,我现在正在R中使用当前的数据帧,我的目标是使用tidyr中的分离功能将songs_流派列分为两部分: 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
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
歌曲您可以使用sep=“\\”
对进行“转义”
是一个特殊的正则表达式字符,它匹配任何字符,除非转义。最好使用分隔符,如。
来避免此问题。您也可以使用包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
require(stringr)
#数据:
有两个词我也有类似的问题,在搜索时发现了这个问题——我使用了分隔符“.”和“|”,但当我试图分隔时,却将每个数字作为一列。使用“\u1”作为分隔符解决了我的问题。谢谢你的建议。
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