I have two data frames, something like this:
data = data.frame(data=cbind(1:12,rep(c(1,2),6),rep(c(1,2,3),4))) colnames(data)=c('v','h','c') lookup = data.frame(data=cbind(c(rep(1,3),rep(2,3)),rep(c(1,2,3),2),21:26)) colnames(lookup)=c('h','c','t')
I want to subtract lookup $ t from the data $ v, where the columns h and c correspond.
I thought something like this would work
data$v-lookup$t[lookup$h==data$h&lookup$c==data$c]
but itβs not magically known that I want to implicitly iterate over data rows
I ended up doing it
myt = c() for(i in 1:12) { myt[i] = lookup$t[lookup$h==data$h[i]&lookup$c==data$c[i]] }
which works fine, but I hope someone can suggest a more reasonable way that doesn't include a loop.
ansate
source share