Adam_G
Adam_G

Reputation: 7879

Create some data frame column names from a list

I am currently creating an empty data frame and then appending each row of values for a group. There will be dozens of columns to add after the initial ones, and I was wondering if it's possible to create some column names from a list:

I know I can create an empty data frame like this:

prssa.predictions <- data.frame("time"=integer(), "diagnosis"=character(), 
                    "n_subj"=integer(), "acknowledge"=double(), "response.elab"=double(),
                    stringsAsFactors = FALSE)

But if I have a list that includes the last two columns (they will all be doubles), can I use that in the data frame creation?

prediction.list <- c("acknowledge", "response.elab")

For completeness' sake, here is what I do with the data frame:

diagnosis.list <- c("AD_m", "DS_m", "FXa_m", "FXo_m", "TD_m")
# loop through all scores and append to prediction df
for (time in c(1,3)) {
  for (diagnosis in diagnosis.list) {
    # subset by time and diagnosis group
    diagnosis.time.subset <- subset(prssa.p1p3, 
                                    ptime==time & group_ASDstatus==diagnosis)

    subj.count <- nrow(diagnosis.time.subset)
    ack.pred <- polr.t.score(diagnosis.time.subset, "acknowledge")

    # append group info and scores to next row of df
    prssa.predictions[nrow(prssa.predictions)+1, ] <- c(time, diagnosis, subj.count,
                                                        ack.pred)

Upvotes: 0

Views: 66

Answers (1)

Julien Navarre
Julien Navarre

Reputation: 7830

Why not adding them with a for loop ?

for (x in prediction.list)
  prssa.predictions[[x]] <- double()

Upvotes: 1

Related Questions