这是基于包rbind.fill.matrix(...)
中的解决方案plyr
。在包含 20,000 行的数据集上,它的运行时间约为 3.6 秒。
# create an sample dataset - you have this already
library(data.table)
words <- LETTERS[1:10] # "words" are just letters in this example
set.seed(1) # for reproducible example
w <- sapply(1:2e4,function(i)paste(words[sample(1:10,sample(1:10,1))],collapse=" "))
dt <- data.table(words=w)
head(dt)
# complaint
# 1: D F H
# 2: I J F
# 3: A B I E C D H
# 4: J D G H B I A E
# 5: A D G C
# 6: F E B J I
# you start here...
library(plyr)
result <- rbind.fill.matrix(lapply(strsplit(dt$words, split=" "),matrix,nr=1))
result <- as.data.table(result)
head(result)
# 1 2 3 4 5 6 7 8 9 10
# 1: D F H NA NA NA NA NA NA NA
# 2: I J F NA NA NA NA NA NA NA
# 3: A B I E C D H NA NA NA
# 4: J D G H B I A E NA NA
# 5: A D G C NA NA NA NA NA NA
# 6: F E B J I NA NA NA NA NA
编辑:根据@Ananda 下面的评论添加了一些基准测试。
f.rfm <- function() as.data.table(rbind.fill.matrix(lapply(strsplit(dt$complaint, split=" "),matrix,nr=1)))
library(splitstackshape)
f.csplit <- function() cSplit(dt, "complaint", " ",type.convert=FALSE)
library(stringi)
f.sl2m <- function() as.data.table(stri_list2matrix(strsplit(dt$complaint, split=" "), byrow = TRUE))
f.ssf <- function() as.data.table(stri_split_fixed(dt$complaint, " ", simplify = TRUE))
all.equal(f.rfm(),f.csplit(),check.names=FALSE)
# [1] TRUE
all.equal(f.rfm(),f.sl2m(),check.names=FALSE)
# [1] TRUE
all.equal(f.rfm(),f.ssf(),check.names=FALSE)
# [1] TRUE
library(microbenchmark)
microbenchmark(f.rfm(),f.csplit(),f.sl2m(),f.ssf(),times=10)
# Unit: milliseconds
# expr min lq median uq max neval
# f.rfm() 3566.17724 3589.31203 3606.93303 3665.4087 3719.32299 10
# f.csplit() 98.05709 102.46456 104.51046 107.9588 117.26945 10
# f.sl2m() 55.45527 55.58852 56.75406 58.9347 67.44523 10
# f.ssf() 17.77499 17.98879 18.30831 18.4537 21.62161 10
所以看起来stri_split_fixed(...)
是赢家。