Here is some sample data.
vv var1 var2
1 a 1/1/2010
1 c 1/3/2010
2 d 1/6/2010
3 a 1/8/2010
3 c 1/9/2010
4 a 1/10/2010
4 b 1/11/2010
5 d 1/13/2010
6 a 1/16/2010
6 b 1/17/2010
7 a 1/19/2010
7 b 1/20/2010
8 d 1/22/2010
9 a 1/25/2010
9 c 1/27/2010
I am trying to create new variables, populated by responses from other variables. I thought this to be easy enough. For example, I tried something like below.
data$new1[data$var1=="a"]<-data$var2
#or
data$new1[data$var1=="b" | data$var1=="c"]<-data$var2
I get the error number of items to replace is not of replacement length
. In my data, not every var1=="a"
has a var2
value, so I am not sure why r is not just assigning NA's for missing values, which is something that I am okay with (rather prefer it actually). Basically, I want r to give new1
NA values for any var1!="a"
.
I also tried
if (data$var1=="a") {data$new1<-data$var2} else {data$new1<-"NA"}
but I get the error the condition has length > 1 and only the first element will be used
.
Now, I think I can subset my data to only have data with var1=="a"
, and then assign my values, and then just merge back into the main data set with the all=T
option to get the NA's imputed, but I really want to avoid doing this.
I am not really sure what the problem is. Any advice greatly appreciated. Cheers.
Output from methods below for running the code:
data$new1 <- ifelse(data$var1 %in% c("b","c"),data$var2,NA)
vv var1 var2 new1
1 1 a 1/1/2010 NA
2 1 c 1/3/2010 12
3 2 d 1/6/2010 NA
4 3 a 1/8/2010 NA
5 3 c 1/9/2010 15
6 4 a 1/10/2010 NA
7 4 b 1/11/2010 3
8 5 d 1/13/2010 NA
9 6 a 1/16/2010 NA
10 6 b 1/17/2010 6
11 7 a 1/19/2010 NA
12 7 b 1/20/2010 8
13 8 d 1/22/2010 NA
14 9 a 1/25/2010 NA
15 9 c 1/27/2010 11