从 Wikipedia 导入表格后,我有以下形式的值列表:
> tbl[2:6]
$`Internet
Explorer`
[1] "30.71%" "30.78%" "31.23%" "32.08%" "32.70%" "32.85%" "32.04%" "32.31%" "32.12%" "34.07%" "34.81%"
[12] "35.75%" "37.45%" "38.65%" "40.63%" "40.18%" "41.66%" "41.89%" "42.45%" "43.58%" "43.87%" "44.52%"
$Chrome
[1] "36.52%" "36.42%" "35.72%" "34.77%" "34.21%" "33.59%" "33.81%" "32.76%" "32.43%" "31.23%" "30.87%"
[12] "29.84%" "28.40%" "27.27%" "25.69%" "25.00%" "23.61%" "23.16%" "22.14%" "20.65%" "19.36%" "18.29%"
我试图摆脱百分比符号,以便将数据转换为数字形式。
有没有比矢量化更快的方法来清理这些数据?我当前的代码如下:
data <- lapply(tbl[2:6], FUN = function(x) as.numeric(gsub("%", "", x)))
数据最终成为数据框,但我无法gsub
在数据框的所有元素上正常工作。有没有办法 gsub() 数据框的每个元素?
该项目的代码是在线的,有结果。提前致谢!