R matching more than 2 conditions and return the response value

前端 未结 4 1837
名媛妹妹
名媛妹妹 2021-01-18 20:25

Hi I have two data set where the first one is a set of index:

ind1<-rep(c(\"E\",\"W\"), times=20)
ind2<-sample(100:150, 40)
y<-c(1:40)
index<-dat         


        
4条回答
  •  一生所求
    2021-01-18 20:27

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

    will give you the x and y values for each matching combination of ind1 and ind2, and x1 and x2. All combinations of x1 and x2 will be kept (even if that combination of ind1 and ind2 doesn't occur in index, but combinations of ind1 and ind2 that don't occur in data will be dropped. As written, the solution will keep x3 and y values, but if you'd like to drop the y values you can use merge(data[ ,-3], ... as per @Ferdinand.kraft 's suggestion.

提交回复
热议问题