I am really new at R and this is probably a really basic question. I have a data set with 9 columns that have information on income data. How do I find the overall percentage of individuals who earn more than 50k?
Asked
Active
Viewed 67 times
0
-
1Welcome to Stack Overflow. I definitely think we can help you with this problem. Take a look at https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example, which explains how to make a great reproducible example with a minimal dataset – Joe Erinjeri Jan 09 '22 at 04:58
1 Answers
1
It is easier to help if you provide some sample data in a reproducible format.
Let's say this is your data
df <- data.frame(id = 1:5, income = c(55000, 12000, 650000, 100000, 32000))
df
# id income
#1 1 55000
#2 2 12000
#3 3 650000
#4 4 100000
#5 5 32000
To count the percentage of people whose income is greater than 50K, you can count the number of people whose income is greater than 50k and divide it by total number of people which can be done as -
perc <- sum(df$income > 50000)/nrow(df) * 100
perc
#[1] 60
Another shorter way to do the same thing in R would be to consider logical values as numbers. So TRUE -> 1
and FALSE -> 0
so to get the percentage we can do -
perc <- mean(df$income > 50000) * 100
perc
#[1] 60

Ronak Shah
- 377,200
- 20
- 156
- 213