This is the code I have set up so far :
library(dslabs)
library(dplyr)
library(lubridate)
data("reported_heights")
dat <- mutate(reported_heights, date_time = ymd_hms(time_stamp)) %>%
filter(date_time >= make_date(2016, 01, 25) & date_time < make_date(2016, 02, 1)) %>%
mutate(type = ifelse(day(date_time) == 25 & hour(date_time) == 8 & between(minute(date_time), 15, 30), "inclass","online")) %>%
select(sex, type, time_stamp)
y <- factor(dat$sex, c("Female", "Male"))
x <- dat$type
counter <- count(dat, sex,type)
It creates for me a tbl_df that looks like this, link below :
sex | type | n
1 Female | inclass | 26
2 Male | inclass | 13
3 Female | online | 42
4 Male | online | 69
I am asking if you can help me with a code that will calculate the proportion of each sex in each type of class.
I have been trying to create a new table using the x characters "inclass" and "online" as columns with a proportion column added and then the y factors "male" and "female" would be the rows. I have been trying to do this using pull()
and prop.table()
but I am a total newbie and it would mean the world to me if you beautiful experts can help me. I have been going through answers for hours now and maybe the answer is already out there so please excuse that I can't seem to find it.... Thank you so much.
What is the proportion of the sexes(male&female) in each type of class(inclass&online)?
It's possible to calculate this by dividing the sex with the total number of students in a given type of class.
For example: There are 42 females studying online out of the total (42+69)=111. Answer: In the online class 38% are females.
How can we do this in R ?