Daniel -4 years ago 168
R Question

# How to identify the objects of a list with >1 rows in R?

I'd like to know how to identify the objects of a

`list`
with
`>1 rows`
in
`R`
?

as example let's say we have the following
`list`
:

`````` c = 6:10
d = 1:5
f = 11:15

output <- list(var1 = rbind(c,d,f), var2 = c, var3 = rbind(d,f))
output

\$var1
[,1] [,2] [,3] [,4] [,5]
c    6    7    8    9   10
d    1    2    3    4    5
f   11   12   13   14   15

\$var2
[1]  6  7  8  9 10

\$var3
[,1] [,2] [,3] [,4] [,5]
d    1    2    3    4    5
f   11   12   13   14   15
``````

The expected is to have a file either list or provide the name of
`object`
with
`> 1`
`row`
as following:

``````output2
\$var1
[,1] [,2] [,3] [,4] [,5]
c    6    7    8    9   10
d    1    2    3    4    5
f   11   12   13   14   15

\$var3
[,1] [,2] [,3] [,4] [,5]
d    1    2    3    4    5
f   11   12   13   14   15
``````

or

``````output2
[1] var1, var3
``````

You can do this with `sapply`, `dim`, isTRUE, and `[` in base R:

``````output[sapply(output, function(i) isTRUE(dim(i)[1] > 1))]
\$var1
[,1] [,2] [,3] [,4] [,5]
c    6    7    8    9   10
d    1    2    3    4    5
f   11   12   13   14   15

\$var3
[,1] [,2] [,3] [,4] [,5]
d    1    2    3    4    5
f   11   12   13   14   15
``````

`sapply` runs through each object and checks if the row dimension, first element of `dim`, is greater than 1. However, as vectors and some other objects (eg, lists) do not have a dimension attribute, this check returns `logical(0)`. To coerce a FALSE out of this, we wrap the output in `isTRUE`. The logical vector resulting from `sapply` is used to subset the list.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download