Reputation: 11
I am learning rstudio for the first time and I have a question in organizing information in a table,
I have three variable, sex(0=man, 1=woman) BP(blood pressure) and Obese, in total I have 102 observations. In the obese variable, values above 1 means you are obese, less than 1 means you are healthy.
I want to construct a table that tells me in one column the number of women that are obese and healthy and in another column the number of man that are obese and healthy.
How do I do that?
Upvotes: 0
Views: 42
Reputation: 1495
if your object is called data
with columns sex
, BP
and Obese
:
library(dplyr)
summary <- data %>%
group_by(sex) %>%
summarise(count_obese = sum(ifelse(Obese > 1, TRUE, FALSE), na.rm = TRUE),
count_healthy = sum(ifelse(Obese <= 1, TRUE, FALSE), na.rm = TRUE))
sum
works as R equates the value TRUE
to 1
and FALSE
to 0
. For example:
> TRUE + FALSE + TRUE
[1] 2
Upvotes: 1
Reputation: 8107
Using the iris dataset:
library(dplyr) # Load package
iris %>% # Take the data
mutate(above_value = if_else(Sepal.Width > 3, "Above", "Below")) %>% # create an toy variable similar to your obese variable
count(Species, above_value) # Count by Species (or gender in your case) and the variable I created above
# A tibble: 6 x 3
Species above_value n
<fct> <chr> <int>
1 setosa Above 42
2 setosa Below 8
3 versicolor Above 8
4 versicolor Below 42
5 virginica Above 17
6 virginica Below 33
Upvotes: 1