Reputation: 13
I have a number of large data frames for which I need to know the number of elements in each row. For example, if my dataframe df looks like
X Y Z A B
Q R S
I would want the following output vector:
5
3
How can I code for this in R?
Upvotes: 1
Views: 693
Reputation: 886938
We can use rowSums
on the non-missing elements (assuming the columns that doesn't have values are NA
)
rowSums(!is.na(df))
If the values are blank ""
instead of NA
, then create the logical matrix with ==
and use rowSums
rowSums(df != "")
Upvotes: 1