Applying gsub to various columns

后端 未结 6 1812
无人及你
无人及你 2020-12-10 04:13

What is the most efficient way to apply gsub to various columns? The following does not work

x1=c(\"10%\",\"20%\",\"30%\")
x2=c(\"60%\",\"50%\",         


        
6条回答
  •  南方客
    南方客 (楼主)
    2020-12-10 05:01

    To clean the % out you can do:

    x[per_col] <- lapply(x[per_col], function(y) as.numeric(gsub("%", "", y)))
    
    x
      x1 x2 x3
    1 10 60  1
    2 20 50  2
    3 30 40  3
    

提交回复
热议问题