I have a large dataset of samples with descriptors of whether the sample is viable - it looks (kind of) like this, where \'desc\' is the description column and \'blank\' ind
Here is an option using set from data.table. It should be faster as the overhead of [.data.table is avoided. We convert the 'data.frame' to 'data.table' (setDT(df1)), loop through the column names of 'df1' (excluding the 'desc' column'), assign the elements to "NA" where the logical condition is 'i' is met.
library(data.table)
setDT(df1)
for(j in names(df1)[-1]){
set(df1, i= which(df1[["desc"]]=="blank"), j= j, value= NA)
}
df1
# desc x y z
# 1: blank NA NA NA
# 2: blank NA NA NA
# 3: sample 4.322014 4.798248 4.995959
# 4: sample 3.997565 5.975604 7.160871
# 5: blank NA NA NA
# 6: blank NA NA NA
# 7: blank NA NA NA
# 8: sample 4.382937 5.926217 5.203737
# 9: sample 4.976908 3.079191 4.614121
#10: blank NA NA NA
Or another option (based on @dww's comment)
setDT(df1, key = "desc")["blank", names(df1)[-1] := NA][]