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

730 Views Asked by At

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

  • 'a' value to any observation where 1 appears in either G or H or appears in both
  • 'b' to observations where 1 appears in either/both of I and J.
  • 'c' to observations where 1 appears in either/both K and L.
1

There are 1 best solutions below

2
On BEST ANSWER

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")