I was hoping somebody could help me with a problem I'm having creating a function. The dataset I'm using contains survey responses, with a column for each question (Q1, Q2, etc) and the responses on each row. The function has to be able to select the column (Q1, Q2, etc) and then filter from within that column for one particular response so that it can count it.
I'm trying to write a function that allows you to include the question number that you want to select as one of the arguments. Here is the code:
my_function <- function(survey, question_number) {
selected_question <- survey %>%
select(question_number)
everyday_responses <- selected_question %>%
filter(question_number == "Every day") %>%
count()
This works for selecting the column but does not work for filtering within that column. I've worked out that this is because I have to input the question_number argument as "Q1" (with quotation marks around it). This is causing the filter(question_number == "Every day") line to not work properly, as this is expecting the column name without the " " (Q1 not "Q1").
Can anybody explain why this is happening and potentially suggest a fix? I'm fairly new to using R, so I may be missing something completely.
Many thanks in advance :D