If I have a datatable and my goal is to change any rows containing MONTH=\"Percent Change:\" to percentage:
MONTH YEAR Client
We can convert the columns 4 to 8 as character
class. Then, using the logical condition in i
, we loop over the columns 4 to 8, paste
the %
and assign (:=
) it back to the columns.
library(data.table)
setDT(df)[, (4:8) := lapply(.SD, as.character), .SDcols= 4:8]
df[MONTH=="Percent Change:", (4:8) :=
lapply(.SD, function(x) paste0(x[!is.na(x)],"%")), .SDcols=4:8]