I have a dataframe that originated from an excel file. It has the usual headers above the columns but some of the columns have % signs in them which I want to remove.
Searching stackoverflow gives some nice code for removing percentages from matrices, Any way to edit values in a matrix in R?, which did not work when I tried to apply it to my dataframe
as.numeric(gsub("%", "", my.dataframe)) instead it just returns a string of "NA"s with a warning message explaining that they were introduced by coercion. When I applied,
gsub("%", "", my.dataframe)) I got the values in "c(...)" form, where the ... represent numbers followed by commas which was reproduced for every column that I had. No % was in evidence; if I could just put this back together ... I'd be cooking.
Any help greatfully received, thanks.
dput(head(...))is often useful.stringsAsFactors = FALSEdoesn't solve the problem because once you read the data and then apply Arun's solution the resulting data.frame will consist of only factors, the numeric elements will be coerced to be factors, so the solution is using Arun's suggestion and addingas.numericinsidesapplycall.