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.
This is a simple solution by creating a variable and then assigning values to it using subsets. Is this sufficient to your purpose?
EDIT: As per the suggestion by David Arenburg, the code gets cleaner and better readable (and probably more efficient) by using
within()
: