根据 R 中的几种模式拆分列
Split column depending on several patterns in R
我需要做的一些数据处理工作是根据几个单词拆分一列。为了简单起见,假设我只有两个词(之前和之后)
示例数据
data <- structure (list(author=c("joe","mack","rick"),options=c("bike before run","car after bike","bus after bike")), class= "data.frame", row.names=c(NA,-3L))
输出:
author new1 new2
joe bike run
mack car bike
rick bus bike
尝试使用 tidyr 无济于事
data %>% tidyr::separate(options, into=c("new1","new2"),sep="after|before")
试试这个:
library(dplyr)
library(reshape)
data %>% cbind(.,colsplit(data$options, "after|before", c("new1", "new2"))) %>%
select(-options)
我需要做的一些数据处理工作是根据几个单词拆分一列。为了简单起见,假设我只有两个词(之前和之后)
示例数据
data <- structure (list(author=c("joe","mack","rick"),options=c("bike before run","car after bike","bus after bike")), class= "data.frame", row.names=c(NA,-3L))
输出:
author new1 new2
joe bike run
mack car bike
rick bus bike
尝试使用 tidyr 无济于事
data %>% tidyr::separate(options, into=c("new1","new2"),sep="after|before")
试试这个:
library(dplyr)
library(reshape)
data %>% cbind(.,colsplit(data$options, "after|before", c("new1", "new2"))) %>%
select(-options)