Search code examples
rdataframeinsertlarge-data

How to insert multiple columns at specific positions in large data frame in R?


I have a large data frame in R with over 200 participants who have answered 152 questions. Now, I want to insert a column based on a conditional query after each "Answer" column. As an example, I have the following data frame:

data <- data.frame(Participants = 1:5,
                   Answer1 = c(4, 6, 2, 2, 3),
                   Answer2 = c(5, 1, 3, 5, 4))

I now want to insert a new conditional column of "Confidence" after each "Answer" column. For the column of "Answer1", the query would look like this:

data$Confidence1 <- ifelse(data$Answer1 == 1 | data$Answer1 == 6, 2, ifelse(data$Answer1 == 2 | data$Answer1 == 5, 1, 0))

In the end, I want the data frame to look like this:

data <- data.frame(Participants = 1:5,
                   Answer1 = c(4, 6, 2, 2, 3),
                   Confidence1 = c(0, 2, 1, 1, 0),
                   Answer2 = c(5, 1, 3, 5, 4),
                   Confidence2 = c(1, 2, 0, 1, 0))

Does anyone have an idea on how to achieve this for all "Answer" columns at once? Thanks!


Solution

  • Here is one solution using the modify() function from the purrr package the response by @r2evans from merge two data table into one, with alternating columns in R

    library(purrr)
    
    # create data, adding one more answer to your example 
    data <- data.frame(Participants = 1:5,
                       Answer1 = c(4, 6, 2, 2, 3),
                       Answer2 = c(5, 1, 3, 5, 4),
                       Answer3 = c(3, 2, 3, 4, 5))
    
    # make a new df containing only the answer columns from data 
    answers <- data[,2:4]
    
    # make confidence df and give it correct names 
    conf <- modify(answers, function(x) ifelse(x == 1 | x == 6, 2, ifelse(x == 2 | x == 5, 1, 0))) 
    names(conf) <- paste0("Confidence",1:ncol(answers))
    
    # set order 
    neworder <- order(c(2*(seq_along(answers) - 1) + 1,
                        2*seq_along(conf)))
    
    # put it together 
    cbind(Participants = data[,1], cbind(answers, conf)[,neworder])