Apologies in advance, but I'm new both here and to R. What I'm trying to do is automate adding a column into a data frame that is filled with the actual name of the data frame. For example, if I have the following data frame:
> q103
a b c d
d 1 4 6 9
e 2 8 3 12
f 3 12 8 16
How can I add a column to the end of it that has the character string q103 in each row (without specifically naming it as such, since I will need to repeat this for several hundred data frames), so that I end up with:
> q103
a b c d X
d 1 4 6 9 q103
e 2 8 3 12 q103
f 3 12 8 16 q103
The problem is that there are a lot of these data frames and they inside a list of lists (e.g., something like List[[list]][["q100277']] is a data frame in the list). Also, their names are somewhat random, but are important to keep (I can't just rename them sequentially). So, I need a way to tell R to basically "look at the name of data frame X and add that character string to a new column in the data frame, then do this for every data frame in the list"). It feels like some sort of lapply would work, but I have no idea what to actually tell it to do in order to get there.
Any help in figuring out how to get a column into each data frame that is just populated by the name of that data frame without doing so manually for each data frame is greatly appreciated!
EDIT: I've tried to create a reproducible example (per comments) below. This will create something similar to what I'm looking at (except the example is a much smaller list!)
library(CTT)
library(dplyr)
library(tidyverse)
library(purrr)
## Create student response patterns for a fake test
q102 <- c("A", "B", "C", "D", "O", "A", "A", "C", "D", "A", "C", "D", "O", "D", "A", "B", "A", "C", "D", "A")
q107 <- c("C", "D", "O", "D", "A", "B", "A", "C", "D", "A", "A", "B", "C", "D", "O", "A", "A", "C", "D", "A")
q1045 <- c("B", "O", "C", "A", "D", "B", "O", "C", "A", "D", "B", "O", "C", "A", "D", "B", "O", "C", "A", "D")
q101 <- c("A", "B", "C", "D", "O", "A", "A", "C", "D", "A", "B", "O", "C", "A", "D", "B", "O", "C", "A", "D")
q1064 <- c("C", "D", "O", "D", "A", "B", "A", "C", "D", "A", "A", "B", "C", "D", "O", "A", "A", "C", "D", "A")
q104 <- c("A", "B", "C", "D", "O", "A", "A", "C", "D", "A", "B", "O", "C", "A", "D", "B", "O", "C", "A", "D")
## Create an assessment key to identify the test
AssessmentKey <- c("ADW", "ADW", "ADW", "ADW", "ADW", "ADW", "ADW", "ADW", "ADW", "ADW", "ADW", "ADW", "ADW", "ADW", "ADW", "ADW", "ADW", "ADW", "ADW", "ADW")
## Assign response pattern to the assessment key
Students1 <- data.frame(q102, q107, q1045, q101, q1064, q104, AssessmentKey)
remove(AssessmentKey)
## Create a second assessment key to identify a different test
AssessmentKey <- c("XYZ", "XYZ", "XYZ", "XYZ", "XYZ", "XYZ", "XYZ", "XYZ", "XYZ", "XYZ", "XYZ", "XYZ", "XYZ", "XYZ", "XYZ", "XYZ", "XYZ", "XYZ", "XYZ", "XYZ")
## Assign the response pattern to the second assessment key
Students2 <- data.frame(q102, q107, q1045, q101, q1064, q104, AssessmentKey)
remove(q102, q107, q1045, q101, q1064, q104, AssessmentKey)
## Create a data frame combining the two different assessments
StudentAnswers <- rbind(Students1, Students2)
## Create a data frame with the answer key for both tests
AnswerKey <- c("A", "B", "A", "A", "C", "D", "A", "B", "A", "A", "C", "D")
QuestionKey <- c("q102", "q107", "q1045", "q101", "q1064", "q104",
"q102", "q107", "q1045", "q101", "q1064", "q104")
AssessmentKey <- c("ADW", "ADW", "ADW", "ADW", "ADW", "ADW", "XYZ", "XYZ", "XYZ", "XYZ", "XYZ", "XYZ")
AnswerKeys <- data.frame(QuestionKey, AnswerKey, AssessmentKey)
remove(AnswerKey, QuestionKey, AssessmentKey)
X <- c("ADW", "XYZ")
y <- lapply(
(X), function(x)
{
## This will filter the data file to a specific assessment and
## select the columns needed for analysis
StudentResponse <- StudentAnswers %>%
dplyr::filter(AssessmentKey == x) %>%
dplyr::select(q102, q107, q1045, q101, q1064, q104)
AKey <- AnswerKeys %>%
dplyr::filter(AssessmentKey == x) %>%
dplyr::select(AnswerKey)
## using safely from the purr package to run the distractorAnalyis
## function from CTT in case of errors
safeDA = safely(.f=distractorAnalysis)
safeDA(StudentResponse, AKey)
}
)
## This part removes the empty "error" data frames from the list generated above.
Z <- c(1:length(y))
Results <- lapply(
(Z), function(Z)
{ y[[Z]][["result"]]
})