I try to delete all "," from a set of variables in my data frame (df). For a single variable (e.g. debt), I successfully run:
df$debt <- as.numeric(gsub(",","",df$debt))
And it exactly does what I want (i.e. overwrites the factor variable "1,324,234.35" into a numeric "1324234.35"). So, I create the following for loop with a variable list:
dflist <- c("debt","fundequity","age","coempl","corev")
for (i in dflist) {
df$i <- as.numeric(gsub(",","",df$i))
}
but when I run it, R returns the following error:
> dflist <- c("debt","fundequity","age","coempl","corev")
> for (i in dflist) {
+ df$i <- as.numeric(gsub(",","",df$i))
+ }
Error in `$<-.data.frame`(`*tmp*`, "i", value = numeric(0)) :
replacement has 0 rows, data has 570225
How can I make it work? Thank you very much in advance.
EDIT: Although similar to this post, the output error is different, plus I need to overwrite my variables. After the proposed solution (see below), it seemed the problem was in the $ sign.