Your issue is your creating of the subset: the subset commands expects a boolean and you gave it a string. ('check'). So the simplest solution here is to add a 'parse'. I feel there is a more elegant way to solve this problem and I hope someone'll come along and do it, but you can fix the final part of your code with the following
mysubset <- subset(df,with(df,eval(parse(text=check)))) if(nrow(mysubset)>0){ mysubset$C <- dt[nr,i] } subdf[[i]]<-mysubset
I have added the parse/eval part to generate a vector of booleans to subset only the 'TRUE' cases, and added a check for whether C could be added (will give error if there are no rows).
Based on the previous answer, I came up with a more elegant/practical way of generating a vector of combined rules, and then applying them all to the data, using apply/lapply.
##create list of formatted rules#format each 'building' block separately, #based on rows in 'dt'.part_conditions <- apply(dt[-nrow(dt),],MARGIN=1,FUN=function(x){ res <- sprintf("(%s%s)", ifelse(x[-1]=="Y","","!"), x[1])})# > part_conditions# 1 2 # [1,] "(A>0)""(B>1)"# [2,] "(A>0)""(!B>1)"# [3,] "(!A>0)""(B>1)"# [4,] "(!A>0)""(!B>1)"#combine to vector of conditionsconditions <- apply(part_conditions, MARGIN=1,FUN=paste, collapse="&")# > conditions# [1] "(A>0)&(B>1)""(A>0)&(!B>1)""(!A>0)&(B>1)""(!A>0)&(!B>1)"#for each condition, test in data wheter condition is 'T'temp <- sapply(conditions, function(rule){ return(with(df, eval(parse(text=rule))))})rules <- as.numeric(t(dt[nrow(dt),-1]))#then find which of the (in this case) four is 'T', and put the appropriate rule#in dfdf$C <- rules[apply(temp,1,which)]> df x A B C1 x1 -1 1 12 x2 1 3 13 x3 0 1 14 x4 2 1 25 x5 2 3 16 x6 -2 1 1