简体   繁体   English

获取列表列的每一行的长度

[英]Get for each row of list-column the length

Problem: I want to find out the number of elements in a list-column (here y ).问题:我想找出列表列中的元素数(这里是y )。 Is there a more efficient and elegant way to determine this rather with purrr::map -function?有没有更有效和优雅的方法来确定这一点,而不是使用purrr::map This is really slow when trying to apply this to a data.table object with more than 700,000 rows.尝试将其应用于具有超过 700,000 行的 data.table 对象时,这真的很慢。

library(data.table)

dt <- data.table(
  x = seq(4),
  y = list(c(1, 2, 3), c(2, 3), c(5, 1), c(12, 90, 200))
)

dt[, class(y)]

## more efficient/elegant way?
dt[, purrr::map(y, length)]

You can do:你可以做:

dt[, lengths(y)]

[1] 3 2 2 3

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

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