Reputation: 5
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:
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
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
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