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?
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?
 
    
     
    
    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 != "")
