Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How count elements in an column that are not empty

Tags:

r

count

I just started with R, and i have to count elements from an column that are not empty. For example :

   exampleColumn
1 "heey"
2
3 "World"
4 "how are you "

How can I achieve this ?

like image 768
user3046636 Avatar asked Feb 22 '14 08:02

user3046636


2 Answers

If you want to count the strings that are not identical to the empty string (""), you can use:

sum(dat$exampleColumn != "")
like image 91
Sven Hohenstein Avatar answered Oct 10 '22 02:10

Sven Hohenstein


@Sven definitely gave the right answer. If you were like me, wondering why

length(dat$exampleColumn != "")

does not work. It's because "length" counts all the TRUE/FALSE evaluations, but "sum" only sums up the TRUE values. A beginner's ah-ha moment!

like image 1
RachelSunny Avatar answered Oct 10 '22 04:10

RachelSunny