按行将data.table转换为向量 [英] convert a data.table to a vector rowwise
问题描述
我有一个data.table,例如:
I have a data.table such as:
example <- data.table(fir =c("A", "B", "C", "A","A", "B", "C"), las=c( "B", "C","B", "C", "B", "C","C"))
A B
B C
C B
A C
A B
B C
C C
虽然我猜问题与data.frame相同。
Though I guess the problem is the same with a data.frame.
,而我想得到一个矢量:
and I would like to get a vector as this:
A, B, B, C, C, B, A, C, A, B, B, C, C, C
我想堆叠左侧的每一行...
That's, I want to stack every row on the left hand side...
我尝试了 unlist(example),但它按列提取数据
I've tried unlist(example) but it extracts the data columnwise instead.
如何获取?
我也尝试过应用,转置和其他奇怪的事情。
How can I get it? I've also tried with apply, transposing and other strange things.
推荐答案
在矩阵以及data.frame / data.table(尽管与矩阵不同),数据按列存储,您可以先对其进行转置:
As in a matrix as well as a data.frame/data.table (though different from a matrix), data is stored column wise, you can transpose it first:
as.vector(t(example))
# [1] "A" "B" "B" "C" "C" "B" "A" "C" "A" "B" "B" "C" "C" "C"
基准测试,包括使用虚拟数据集的@ Sotos,@ Frank和@Wen提供的选项:
A benchmark testing including options provided by @Sotos, @Frank and @Wen using a dummy data set:
example <- as.data.table(matrix(sample(LETTERS, 10^7, replace = T), ncol = 1000))
dim(example)
#[1] 10000 1000
library(microbenchmark)
psidom <- function() as.vector(t(example))
sotos <- function() c(t(example))
frank <- function() unlist(transpose(example), use.names = FALSE)
wen <- function() unname(unlist(data.frame(t(example))))
# data.table 1.10.4
microbenchmark(psidom(), sotos(), frank(), wen(), times = 10)
#Unit: milliseconds
# expr min lq mean median uq max neval
# psidom() 163.5993 178.9236 393.4838 198.6753 632.1086 1352.012 10
# sotos() 186.8764 188.3734 467.2117 343.1514 618.3121 1221.721 10
# frank() 3065.0988 3493.3691 5315.4451 4649.4643 5742.2399 9560.642 10
# wen() 7316.6743 8497.1409 9200.4397 9038.2834 9631.5313 11931.075 10
data.table dev版本1.10.5中的另一项测试:
Another test in data.table dev version 1.10.5:
# data.table 1.10.5
psidom <- function() as.vector(t(example))
sotos <- function() c(t(example))
frank <- function() unlist(transpose(example), use.names = FALSE)
fast <- function() `attributes<-`(t(example), NULL)
microbenchmark(psidom(), sotos(), frank(), fast(), times = 10)
#Unit: milliseconds
# expr min lq mean median uq max neval
# psidom() 228.1248 246.4666 271.6772 256.9131 287.5072 354.2053 10
# sotos() 254.3512 280.2504 315.3487 322.5726 344.7125 390.3482 10
# frank() 290.5476 310.7076 374.6267 349.8021 431.8451 491.9301 10
# fast() 159.6006 167.6316 209.8363 196.8821 272.4758 281.3146 10
这篇关于按行将data.table转换为向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!