简体   繁体   中英

Getting the nrow() of 3 matrices simultaneously in R?

Suppose I have the following 3 matrices:

p.b7.4 = matrix(1:4, nrow = 2) 
p.b6.4 = matrix(1:6, nrow = 3) 
p.b5.4 = matrix(1:8, nrow = 4) 

If I (further) vectorize nrow() in R:

vec.nrow = Vectorize(function(x) nrow(x), "x")

Is it then possible to get the number of rows for the 3 matrices above simultaneously?

I tried the following without success:

vec.nrow(noquote(paste0("p.b", 7:5, rep(".", 3), rep(4, 3))))

Use mget to put all matrices in a list and then use lapply (also try sapply ) to loop through them to obtain NROW

lapply(mget(ls(pattern = "p.b")), NROW)
$p.b5.4
[1] 4

$p.b6.4
[1] 3

$p.b7.4
[1] 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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