如何计算行数? [英] How to count rows?

查看:153
本文介绍了如何计算行数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的问题(至少对我来说不是.).我已经结束了列表,并列出了多个不同长度的数据帧.我想计算每个数据帧的长度.

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

我的验证码不正确

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...

推荐答案

尝试一下:

myvec <- sapply(mylist, NROW)

length在data.frames中给出奇数"结果,因为data.frames实际上是相同长度向量的列表. length(data.frame)为您提供了基础列表的长度,即data.frame的列数.

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.

NROWNCOL很好,因为它们倾向于为大多数对象提供预期"结果.我经常交互使用它们,但是在编写稳定的代码(例如程序,程序包)时回落到nrowncollength,因为它们避免了一些额外的函数调用的开销.

NROW and NCOL are nice because they tend to give "expected" results for most objects. I use them a lot interactively, but fall back to nrow, ncol, and length when writing stable code (e.g. programs, packages) because they avoid the overhead of a few extra function calls.

这篇关于如何计算行数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆