user3046636
user3046636

Reputation: 693

How count elements in an column that are not empty

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 ?

Upvotes: 9

Views: 36529

Answers (2)

RachelSunny
RachelSunny

Reputation: 1134

@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!

Upvotes: 1

Sven Hohenstein
Sven Hohenstein

Reputation: 81693

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

sum(dat$exampleColumn != "")

Upvotes: 16

Related Questions