I've got to get the frequency of one specific value of one single dichotomous variable, preferable using dplyr
but I'll gladly accept alternative solutions. It should just be as short and straightforward as possible.
Here's the example:
dat<-data.frame(x=c(1,1,0,0,NA,NA))
What I could do is this...
dat %>% group_by(x) %>% summarise(sum(!is.na(x)))
...based on what @akrun suggested in another thread.
The issues with this is that it returns a tibble showing the count for each value x
takes:
# A tibble: 3 x 2
x `sum(!is.na(x))`
<dbl> <int>
1 0. 2
2 1. 2
3 NA 0
What I need is just one number for a specific value of x
, say x==1
. Adding this condition (x==1
) to the dplyr
command, however, won't work, as it only returns the same tibble output as above.
Simply put, I need a command that returns the count of x==1
or x==2
and just that. So in this case, the perfect R output would look like this:
[1] 2
I've also tried something like this...
!is.na(dat[,c("x")]==1)
which returns an integer that equals TRUE
if x==1
and FALSE
otherwise. But then I'd need to count the TRUE
s.