'Calculating percentages from datasets

This is probably a basic question but if there is a dataset with two of the columns being asthma (y/n) and gender(m/f). How would I go about finding the percentage of males in the dataset with asthma (and then the percentage of females with asthma subsequently)?

r


Solution 1:[1]

First make some data that matches your characteristics:

set.seed(42)
example <- data.frame(asthma=sample(c("n", "y"), 100, replace=TRUE), gender=sample(c("f", "m"), 100, replace=TRUE))
str(example)
# 'data.frame': 100 obs. of  2 variables:
#  $ asthma: chr  "n" "n" "n" "n" ...
#  $ gender: chr  "m" "m" "f" "m" ...

Now create a table:

(example.tbl <- xtabs(~gender+asthma, example))
#        asthma
#  gender  n  y
#       f 18 27
#       m 26 29

Finally, percentages:

round(prop.table(example.tbl, 1) * 100, 2)
#       asthma
# gender     n     y
#      f 40.00 60.00
#      m 47.27 52.73

You can read the manual pages for each of these functions by typing the command help(function_name) or ?function_name.

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 dcarlson