Juan D
Juan D

Reputation: 5

Iterating through columns in a data.frame to count its values

I have a data.frame named "ant" like this (the original has 217 rows and 22 columns):

V1  V2  V3  V4  V5
No  No  No  Sí  No
No  No  No  Sí  No
Sí  No  No  Sí  No
No  No  No  No  No
No  No  No  Sí  No
No  No  No  Sí  No

I want to:

  1. Count how many "Sí" and "No" are in each column (variable).
  2. Store that info in a new data.frame like this.
  3. And finally plot this new data.frame as a barplot.

So far I have this for the first step:

for(i in 1:ncol(ant)) {
  ant %>% count(i)
  }

But I got an error:

Error: Must group by variables found in `.data`.
* Column `i` is not found.

And I don't know how to proceed.

Also, any NA and/or blank spaces present in the data.frame would affect the calculation?

I appreciate your help, thanks.

Upvotes: 0

Views: 59

Answers (2)

Karthik S
Karthik S

Reputation: 11594

Using base R:

> as.data.frame(rbind(apply(df, 2, function(x) sum(x == 'Sí', na.rm = T)), apply(df, 2, function(x) sum(x == 'No', na.rm = T))), row.names = c('Sí','No'))
   V1 V2 V3 V4 V5
Sí  1  0  0  5  0
No  5  6  6  1  6
> 

Upvotes: 1

john
john

Reputation: 1036

Try this - It uses rlang::sym

df <- data.frame()
for(i in names(ant)) {
  d <- ant %>% count(!!sym(i))
  df <- rbind(df, d)
}

Upvotes: 0

Related Questions