假设您有一个具有以下结构的数据框:
df <- data.frame(a=c(1,2,3,4), b=c("job1;job2", "job1a", "job4;job5;job6", "job9;job10;job11"))
其中列b
是以分号分隔的列表(按行不平衡)。理想的data.frame将是:
id,job,jobNum
1,job1,1
1,job2,2
...
3,job6,3
4,job9,1
4,job10,2
4,job11,3
我有一个部分解决方案需要近2个小时(170K行):
# Split the column by the semicolon. Results in a list.
df$allJobs <- strsplit(df$b, ";", fixed=TRUE)
# Function to reshape column that is a list as a data.frame
simpleStack <- function(data){
start <- as.data.frame.list(data)
names(start) <-c("id", "job")
return(start)
}
# pylr!
system.time(df2 <- ddply(df, .(id), simpleStack))
这似乎是一个尺寸问题,因为如果我运行
system.time(df2 <- ddply(df[1:4000,c("id", "allJobs")], .(id), simpleStack))
只需9秒钟。首先使用sapply(使用不同的函数)转换为一组data.frames很快,但所需的`rbind'需要更长的时间。
答案 0 :(得分:12)
#Split by ; as before
allJobs <- strsplit(df$b, ";", fixed=TRUE)
#Replicate a by the number of jobs in each case
n <- sapply(allJobs, length)
id <- rep(df$a, times = n)
#Turn allJobs into a vector
job <- unlist(allJobs)
#Retrieve position of each job
jobNum <- unlist(lapply(n, seq_len))
#Combine into a data frame
df2 <- data.frame(id = id, job = job, jobNum = jobNum)
答案 1 :(得分:6)
cSplit
旨在处理这种数据操作。
这是针对这个问题的行动:
df <- data.frame(a=c(1,2,3,4),
b=c("job1;job2", "job1a", "job4;job5;job6", "job9;job10;job11"))
# install.packages("splitstackshape")
library(splitstackshape)
cSplit(df, "b", ";", "long", makeEqual = FALSE)
# a b_new
# 1: 1 job1
# 2: 1 job2
# 3: 2 job1a
# 4: 3 job4
# 5: 3 job5
# 6: 3 job6
# 7: 4 job9
# 8: 4 job10
# 9: 4 job11
您也可以在“dplyr”中使用strsplit
,然后使用“tidyr”中的unnest
进行跟进,如下所示:
library(dplyr)
library(tidyr)
df %>%
mutate(b = strsplit(as.character(b), ";", fixed = TRUE)) %>%
unnest(b)
# a b
# 1 1 job1
# 2 1 job2
# 3 2 job1a
# 4 3 job4
# 5 3 job5
# 6 3 job6
# 7 4 job9
# 8 4 job10
# 9 4 job11