I have some data
data
Y X1 X2
1 21.0 160.0 2.620
2 21.0 160.0 2.875
3 22.8 108.0 2.320
4 21.4 258.0 3.215
5 18.7 360.0 3.440
6 18.1 225.0 3.460
7 14.3 360.0 3.570
8 24.4 146.7 3.190
9 22.8 140.8 3.150
10 19.2 167.6 3.440
11 17.8 167.6 3.440
12 16.4 275.8 4.070
13 17.3 275.8 3.730
14 15.2 275.8 3.780
15 10.4 472.0 5.250
16 10.4 460.0 5.424
17 14.7 440.0 5.345
18 32.4 78.7 2.200
19 30.4 75.7 1.615
20 33.9 71.1 1.835
21 21.5 120.1 2.465
22 15.5 318.0 3.520
23 15.2 304.0 3.435
24 13.3 350.0 3.840
25 19.2 400.0 3.845
26 27.3 79.0 1.935
27 26.0 120.3 2.140
28 30.4 95.1 1.513
29 15.8 351.0 3.170
30 19.7 145.0 2.770
31 15.0 301.0 3.570
32 21.4 121.0 2.780
and a list of condition:
a
[[1]]
[1] "X2>=2.393"
[[2]]
[1] "X2< 2.393"
[[3]]
[1] "X2>=2.393" "X1>=266.9"
[[4]]
[1] "X2>=2.393" "X1< 266.9"
How could I create new dummy variable in my data according to these conditions without write by hand each time data$X2>=2.393, etc? How can I merge condition in string with data frame?
My solution was:
> R1<-as.numeric(I( data$X2>=2.393))
> R2<-as.numeric(I( data$X2< 2.393))
> R3<-as.numeric(I( data$X2>=2.393)*I( data$X1>=266.9))
> R4<-as.numeric(I( data$X2>=2.393)*I( data$X1<266.9))
> data=cbind(data,R1,R2,R3,R4)
but want to automatise this procedure.
Thanks a lot.
We could use eval(parse
res <- do.call(cbind, lapply(a, function(x) sapply(x,
function(y) with(data, +eval(parse(text=y))))))
If we need specifically for a[[3]]
res1 <- do.call(cbind, lapply(a[[3]], function(x) with(data, +eval(parse(text=x)))))
If we assume that the list
elements having more than one string should be having &
condition, then
a1 <- lapply(a, paste, collapse=" & ")
res <- do.call(cbind, lapply(a1, function(x) sapply(x,
function(y) with(data, +eval(parse(text=y))))))
unname(colSums(res))
#[1] 25 7 14 11
a <- list("X2>=2.393", "X2< 2.393", c("X2>=2.393", "X1>=266.9"),
c( "X2>=2.393", "X1< 266.9"))