gender = c("M","M","F","F","F")
workplace = c("School","School","Hospital","Hospital","Clinic")
q1 = factor(c(6,5,5,9,9))
q2 = factor(c(6,7,5,9,9))
df = data.frame(gender, workplace, q1, q2)
library(tidyverse)
data4crs_sex <- df %>%
select(gender, starts_with("q"))
result <- data4crs_sex %>%
pivot_longer(cols = -gender,
names_to = "item",
values_to = "value") %>%
group_by(gender, item, value) %>%
tally()
Above I created a dataset including 2 categorical variables, q1 and q2 are two questions in a survey, and the numbers within each question represent the choice. I wanted to count the the number of values in q1 and q2. The code works well. However, when I tried to create a function to try to be easier to get the result just by changing a variable, it failed.
myfun <- function(myvar){
data4crs_sex <- df %>%
select(myvar, starts_with("q"))
result <- data4crs_sex %>%
pivot_longer(cols = -myvar,
names_to = "item",
values_to = "value") %>%
group_by(myvar, item, value) %>%
tally()
return(result)
}
myfun(workplace)
The error showed below:
Error in select()
:
! Can't subset columns that don't exist.
✖ Columns School
, School
, Hospital
, Hospital
, and Clinic
don't exist.
Could you please explain what wrong is my code for that function? Thank you in advance.