rvariable-assignmentmultiple-variable-return

assign multiple categorical values to series of data frame variables in r


Say I have the following data input into R

G <- c(1,1,0,0,0,0,0,0,0) 
H <- c(0,1,1,0,0,0,0,0,0) 
I <- c(0,0,0,0,1,1,0,0,0) 
J <- c(0,0,0,1,0,1,0,0,0) 
K <- c(0,0,0,0,0,0,1,1,0) 
L <- c(0,0,0,0,0,0,0,1,1)

list <- data.frame(G,H,I,J,K,L)

I want to assign


Solution

  • This is a simple solution by creating a variable and then assigning values to it using subsets. Is this sufficient to your purpose?

    list$Z <- NA
    list$Z[list$G|list$H] <- "a"
    list$Z[list$I|list$J] <- "b"
    list$Z[list$K|list$L] <- "c" 
    list
    

    EDIT: As per the suggestion by David Arenburg, the code gets cleaner and better readable (and probably more efficient) by using within():

    list$Z <- NA
    within(list, Z[G|H]<-"a"; Z[I|J]<-"b"; Z[K|L]<-"c")