I'm trying to calculate some statistic called FAR for each group in my data. I wrote a function for calculating the statistic, and it looks like this:
FAR <- function(data){
FAs = sum(data$response %in% c(0,1) & data$correct_response=="No")
NF = sum(data$correct_response=="No")
return(FAs/NF)
}
My data frame is called subjects_no_imagery, and it has a column called random_participant_ID that I'd like to group by. I then want to calculate FAR for each group, like so:
subjects_no_imagery %>% group_by(random_participant_ID) %>% dplyr::summarize(far = FAR())
I keep getting the following error:
subjects_no_imagery %>% group_by(random_participant_ID) %>% dplyr::summarize(far = FAR()) Error: Problem with
summarise()
inputfar
. x argument "data" is missing, with no default ℹ Inputfar
isFAR()
. ℹ The error occured in group 1: random_participant_ID = "2X2DPZG20H". Runrlang::last_error()
to see where the error occurred.
If I pass the data frame as an argument:
subjects_no_imagery %>% group_by(random_participant_ID) %>% dplyr::summarize(FAR = FAR(subjects_no_imagery))
then it runs without error, but I get this output, where instead of having a different RAF for each participant, all of the FARs are the same (and equal the the average across all the participants):
random_participant_ID FAR
<chr> <dbl>
1 2X2DPZG20H 0.196
2 4GF789C0F0 0.196
3 93JKYDEZVY 0.196
4 9J9QM4C7LV 0.196
The command
subjects_no_imagery %>% group_by(random_participant_ID) %>% dplyr::summarize(FAR = FAR(subjects_no_imagery %>% group_by(random_participant_ID)))
also produces the same result.
Any ideas how I can get the FAR statistic for each group?