I have a dataframe in R that I would like to convert all columns (outside the ids) from negative to zero
id1 id2 var1 var2 var3 -1 -1 0 -33 5
We can use data.table
data.table
setDT(d1) for(j in grep('^var', names(d1))){ set(d1, i= which(d1[[j]]<0), j= j, value=0) } d1 # id1 id2 var1 var2 var3 # 1: -1 -1 0 0 5 # 2: -1 -2 9 0 0