0

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.

martins
  • 441
  • 1
  • 5
  • 19

1 Answers1

1

You can't access each column with df$i with i being a variable. You could use the apply family like such:

df[,dflist] = apply(df[,dflist],2,function(x) as.numeric(gsub(",","",x)))

If you want to stick to loops, you can use

for (i in dflist){
    df[,i]=as.numeric(gsub(",","",df[,i]))
}
boski
  • 2,437
  • 1
  • 14
  • 30