DataTable in R, formatting rows with specific value category to a percentage

て烟熏妆下的殇ゞ 提交于 2019-12-01 14:13:34

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]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!