Issue
I have a data set in R that looks very much like your stereotypical 2 column data frame. I.E.:
Question | Value
Q1 | 4
Q1 | 0
Q1 | 4
Q2 | 1
Q2 | 2
Q2 | 4
And I need to input this into the cronbach.alpha function from the ltm package. But to do that, I'll need my data to look like LSAT sample data in R. So something like this:
Q1 | Q2
4 | 1
0 | 2
4 | 4
How can I do that?
Solution
One approach is to put your data into wide format, so each question will be its own column.
Before pivoting your data, you can add row numbers, otherwise rows will not be unique sharing the same question number.
Note this approach will work with uneven numbers of questions (e.g., 10 Q1, but only 8 Q2). If you use the cronbach.alpha
function, you would get an error as there would appear to be "missing data". You can set na.rm = TRUE
to use completed cases (if that is appropriate).
library(tidyverse)
library(ltm)
df_wide <- df %>%
group_by(Question) %>%
mutate(rn = row_number()) %>%
pivot_wider(names_from = Question, values_from = Value) %>%
dplyr::select(-rn)
df_wide
Q1 Q2
<int> <int>
1 4 1
2 0 2
3 4 4
cronbach.alpha(df_wide)
Answered By - Ben Answer Checked By - Terry (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.