R:data.frame列中的分割不平衡列表 [英] R: Split unbalanced list in data.frame column

查看:101
本文介绍了R:data.frame列中的分割不平衡列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你有一个具有以下结构的数据框架:

Suppose you have a data frame with the following structure:

df <- data.frame(a=c(1,2,3,4), b=c("job1;job2", "job1a", "job4;job5;job6", "job9;job10;job11"))

其中列 b 是以分号分隔的列表(按行不平衡)。理想的数据框架将是:

where the column b is a semicolon-delimited list (unbalanced by row). The ideal data.frame would be:

id,job,jobNum
1,job1,1
1,job2,2
...
3,job6,3
4,job9,1
4,job10,2
4,job11,3

我有一个部分解决方案需要将近2小时(170K行):

I have a partial solution that takes almost 2 hours (170K rows):

# 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))

这似乎是一个大小问题,因为如果我运行

It appears to be a size issue, because if I run

system.time(df2 <- ddply(df[1:4000,c("id", "allJobs")], .(id), simpleStack))

只需要9秒钟。首先将数据转换成一组数据。(具有不同的功能)是快速的,但所需的`rbind'需要更长的时间。

it only takes 9 seconds. First converting to a set of data.frames with sapply (with a different function) is fast, but the required `rbind' takes even longer.

推荐答案

#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)

这篇关于R:data.frame列中的分割不平衡列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆