michalk
michalk

Reputation: 1587

Get number of rows of all variables

I have a dataset in few csv files (each csv is a set of different columns but with the same number of rows). After importing all csv files to R I want to merge this data into one table.

As a progress check I would like to check if all imported files have the same number of rows, just in case.

So far I figure out this part:

lapply(lapply(ls(),get),nrow)

Unfortunately this way I'm getting numbers of rows without variables names.

Is there a way to extract number of rows together with variable names?

Thank you in advance for help.

Upvotes: 1

Views: 81

Answers (1)

Julius Vainora
Julius Vainora

Reputation: 48231

Check

a <- matrix(0, 3)
b <- matrix(0, 2)
setNames(lapply(lapply(ls(),get),nrow), ls())
$a
[1] 3

$b
[1] 2

You might also prefer

setNames(sapply(mget(ls()),nrow), ls())
a b 
3 2

Upvotes: 2

Related Questions