I spent one day looking for this answer and I'm almost giving up. Actually, I really imagine is a pretty simple situation, but I'll be glad of any help.
Let's say I have two datasets, the first get all ID of all students
library(tidyverse)
library(psych)
ds_of_students <- data.frame(id=(1:4), school=c("public","private"))
The second one has all the results of a test. Let's say each column is an ID.
ds_of_results <- structure(list(i1 = c(1, 2, 4, 4),
i2 = c(3, 3, 2, 2),
i3 = c(2, 3, 3, 5),
i4 = c(4, 1, 3, 2)),
class = c("tbl_df", "tbl",
"data.frame"), row.names = c(NA, -4L))
And now I need to report a table of students ID , groupped by school, and they results (Actually, It's a Cronbach alpha results, what is pretty common in Psychology).
ds_of_students %>%
group_by(school) %>%
summarise(n=n(),
id = paste(id, collapse = ",")) %>%
mutate(item2=psych::alpha(ds_of_results[c(id)])$total[1])
I've got this message
Error in mutate_impl(.data, dots) :
Evaluation error: Columns `2,4`, `1,3` not found.
But When I run in the traditional fashion, it works
psych::alpha(ds_of_results[c(1,3)])$total[1]
I've tried to work with paste, noquote, gsub ans strcol
Please, run this code to have reproducible results. Thanks much!
library(tidyverse)
library(psych)
ds_of_students <- data.frame(id=(1:4), school=c("public","private"))
ds_of_results <- structure(list(i1 = c(1, 2, 4, 4),
i2 = c(3, 3, 2, 2),
i3 = c(2, 3, 3, 5),
i4 = c(4, 1, 3, 2)),
class = c("tbl_df", "tbl",
"data.frame"), row.names = c(NA, -4L))
ds_of_students %>%
group_by(school) %>%
summarise(n=n(),
id = paste(id, collapse = ",")) %>%
mutate(item2=psych::alpha(ds_of_results[c(id)])$total[1])
alpha(ds_of_results[c(1,3)])$total[1]
My desired output is something like that
And just to give some reality to my question, that's the real dataset, where I have to compute the Cronbach's alpha item the items of each group.