'In R, calculate % of rows that are true
How to calculate % of rows that are true in a data set? See example data frame and output below dataframe
ID Present
1 True
2 True
3 True
4 False
5 False
example output 1
60%
example output 2
.60
Solution 1:[1]
Using base R you could do:
mean(as.logical(df$Present))
[1] 0.6
You can multiply that by 100
prop.table(table(df$Present))
or even:
mosaic::tally(~Present, df, format = 'proportion')
Present
False True
0.4 0.6
mosaic::tally(~Present, df, format = 'percent')
Present
False True
40 60
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
