如何计算行数?
我有一个简单的问题(至少对我来说不是......)。我已经以包含多个不同长度的数据帧的列表结束。我想计算每个数据帧的长度..
mylist <- list (a = data.frame(i = 1:10, j= 11:20), b = data.frame(i = 1:5, j= 11:15), c = data.frame(i = 1:8, j= 11:18))
mylist
$a
i j
1 1 11
2 2 12
3 3 13
4 4 14
5 5 15
6 6 16
7 7 17
8 8 18
9 9 19
10 10 20
$b
i j
1 1 11
2 2 12
3 3 13
4 4 14
5 5 15
$c
i j
1 1 11
2 2 12
3 3 13
4 4 14
5 5 15
6 6 16
7 7 17
8 8 18
我糟糕的代码:
lapply(mylist, function(y)length(y)
$a
[1] 2
$b
[1] 2
$c
[1] 2
哦......不...... 如何计算每个组件数据框中的行数并获得一个新向量:
# number of rows in each component dataframe of the list
myvec
[1] 10 5 8
谢谢您的时间...我很感激...
I have simple question (at least not for me ..). I have been ended with list with multiple dataframes of different length. I want to count length of each dataframe..
mylist <- list (a = data.frame(i = 1:10, j= 11:20), b = data.frame(i = 1:5, j= 11:15), c = data.frame(i = 1:8, j= 11:18))
mylist
$a
i j
1 1 11
2 2 12
3 3 13
4 4 14
5 5 15
6 6 16
7 7 17
8 8 18
9 9 19
10 10 20
$b
i j
1 1 11
2 2 12
3 3 13
4 4 14
5 5 15
$c
i j
1 1 11
2 2 12
3 3 13
4 4 14
5 5 15
6 6 16
7 7 17
8 8 18
My poor codes:
lapply(mylist, function(y)length(y)
$a
[1] 2
$b
[1] 2
$c
[1] 2
Oh....no.....
How can I count number of rows in each component dataframe and get a new vector with:
# number of rows in each component dataframe of the list
myvec
[1] 10 5 8
Thank you for your time...I appreciate it...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
length
使用 data.frames 给出“奇数”结果,因为 data.frames 实际上是相同长度的向量列表。length(data.frame)
为您提供基础列表的长度,即 data.frame 的列数。NROW
和NCOL
很好,因为它们往往会为大多数对象提供“预期”结果。我经常以交互方式使用它们,但在编写稳定代码(例如程序、包)时会回退到 nrow、ncol 和 length,因为它们避免一些额外函数调用的开销。Try this:
length
gives "odd" results with data.frames because data.frames are really lists of vectors of the same length.length(data.frame)
is giving you the length of the underlying list, which is the number of columns of the data.frame.NROW
andNCOL
are nice because they tend to give "expected" results for most objects. I use them a lot interactively, but fall back tonrow
,ncol
, andlength
when writing stable code (e.g. programs, packages) because they avoid the overhead of a few extra function calls.将返回列表中每个元素的长度。注意“长度”(复数)
will return the length of each element in the list. Note "lengths" (plural)
可以找到数组中组件的总数或元素的长度,如变量“q”所示。要查找数组或列表中的行数,请使用函数 GetLength(),输入值为 0,对于行数,我们可以使用相同的函数,输入值为 1。
The overall no of components or the length of elements in an array can be found as indicated for variable 'q'. To find the no of rows in a array or list use the function GetLength() with 0 as input value and for no of rows we can use the same function with the intake value as 1.