After importing a table from Wikipedia, I have a list of values of the following form:
> 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%"
I am trying to get rid of the percentage signs, in order to convert the data to numeric form.
Is there a quicker way to clean this data than going for a vectorization? My current code follows:
data <- lapply(tbl[2:6], FUN = function(x) as.numeric(gsub("%", "", x)))
The data eventually become a data frame, but I could not get gsub
to work properly across all elements of a data frame. Is there a way to gsub() each element of a data frame?
The code for the project is online, with results. Thanks in advance!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…