rnorouzian
rnorouzian

Reputation: 7517

looping over variables of a data.frame leading one final data.frame in R

I have written a function to change any one variable (i.e., column) in a data.frame to its unique levels and return the changed data.frame.

I wonder how to change multiple variables at once using my function and get one final data.frame with all the changes?

I have tried the following, but this gives multiple data.frames while only the last data.frame is the desired output:

data <- data.frame(sid = c(33,33, 41), pid = c('Bob', 'Bob', 'Jim'))

#== My function for ONE variable:
f <- function(data, what){
 data[[what]] <- as.numeric(factor(data[[what]], levels = unique(data[[what]])))
 return(data)
}  

# Looping over `what`:
what <- c('sid', 'pid')
lapply(seq_along(what), function(i) f(data, what[i]))

Upvotes: 1

Views: 36

Answers (1)

akrun
akrun

Reputation: 887901

In the function, we could change to return the data[[what]]

f <- function(data, what){
   data[[what]] <- as.numeric(factor(data[[what]], levels = unique(data[[what]])))
   data[[what]]
  }  

data[what] <- lapply(seq_along(what), function(i) f(data, what[i]))

Or do

data[what] <-  lapply(what, function(x) f(data, x))

Or simply

data[what] <- lapply(what, f, data = data)

Upvotes: 1

Related Questions