0

I have n data frames, each corresponding to data from a city.

There are 3 variables per data frame and currently they are all factor variables.

I want to transform all of them into numeric variables.

I have started by creating a vector with the names of all the data frames in order to use in a for loop.


cities <- as.vector(objects())


for ( i in cities){

i <-  as.data.frame(lapply(i, function(x) as.numeric(levels(x))[x]))

}


Although the code runs and there I get no error code, I don't see any changes to my data frames as all three variables remain factor variables.

The strangest thing is that when doing them one by one (as below) it works:


df <- as.data.frame(lapply(df, function(x) as.numeric(levels(x))[x]))

Seni
  • 93
  • 1
  • 8
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. You are only changing the value of the variable `i`. Multiple variable names can point to the same value. You generally can't update the underlying value for a variable using a different variable name. In you you would generally map a function over a collection to get an new collection. – MrFlick May 07 '19 at 14:32

2 Answers2

1

What you're essentially trying to do is modify the type of the field if it is a factor (to a numeric type). One approach using purrr would be:

library(purrr)

map(cities, ~ modify_if(., is.factor, as.numeric))

Note that modify() in itself is like lapply() but it doesn't change the underlying data structure of the objects you are modifying (in this case, dataframes). modify_if() simply takes a predicate as an additional argument.

Johnny
  • 285
  • 1
  • 7
0

for anyone who's interested in my question, I worked out the answer:


for ( i in cities){ 

  assign(i, as.data.frame(lapply(get(i), function(x) as.numeric(levels(x))[x]))) 

  }

Seni
  • 93
  • 1
  • 8