match - R matching more than 2 conditions and return the response value -


hi have 2 data set first 1 set of index:

ind1<-rep(c("e","w"), times=20) ind2<-sample(100:150, 40) y<-c(1:40) index<-data.frame(cbind(ind1, ind2, y)) 

the second data set 1 needs indexed.

x1<-sample(c("e","w","n"), 40, replace=true) x2<-sample(100:150, 40) x3<-rep(0, times=40) data<-data.frame(cbind(x1,x2,x3)) 

i indicate in x3 x1 , x2 in data matched ind1 , ind2 in index respectively , return corresponding y.

index1<-split(index, index$ind1) data1<-split(data, data$x1) data1$e$x3<-match(data1$e$x2, index1$e$ind2) data1$w$x3<-match(data1$w$x2, index1$w$ind2) 

it kinda matched way wanted did not return y correctly. part did wrong? thanks.

also, there faster/smarter way of doing it? because might have more conditions match with. tried if else statement didn't work.

merge(data, index, by.x=c("ind1", "ind2"), by.y=c("x1", "x2"), all.x=true, all.y=false) 

will give x , y values each matching combination of ind1 , ind2, , x1 , x2. combinations of x1 , x2 kept (even if combination of ind1 , ind2 doesn't occur in index, combinations of ind1 , ind2 don't occur in data dropped. written, solution keep x3 , y values, if you'd drop y values can use merge(data[ ,-3], ... per @ferdinand.kraft 's suggestion.


Comments

Popular posts from this blog

java - JavaFX 2 slider labelFormatter not being used -

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -