简体   繁体   English

将所有data.table行转换为列表R.

[英]Turn all data.table rows into a list R

Start with the following data.table: 从以下data.table开始:

set.seed(1234)
dt <- data.table(x = runif(3), y = runif(3), z = runif(3))

print(dt)
#         x         y           z
#1: 0.1137034 0.6233794 0.009495756
#2: 0.6222994 0.8609154 0.232550506
#3: 0.6092747 0.6403106 0.666083758

And turn it into a list in the following structure: 并将其转换为以下结构中的列表:

print(dt2)
#[[1]]
#[1] 0.1137034 0.6233794 0.009495756
#
#[[2]]
#[1] 0.6222994 0.8609154 0.2325505
#
#[[3]]
#[1] 0.6092747 0.6403106 0.6660838

I've been studying the answers to this question , but have not been able to figure out how to do it for all rows of a data.table at once without applying a looping function. 我一直在研究这个问题的答案,但是没有能够在不应用循环函数的情况下立即弄清楚如何对data.table的所有行执行此操作。 I'm trying to avoid a looping function because of the number of rows in the actual data.table. 我正在尝试避免循环函数,因为实际data.table中的行数。

You can use the data.table::transpose() function: 您可以使用data.table::transpose()函数:

transpose(as.list(dt))

#[[1]]
#[1] 0.113703411 0.623379442 0.009495756

#[[2]]
#[1] 0.6222994 0.8609154 0.2325505

#[[3]]
#[1] 0.6092747 0.6403106 0.6660838

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM