Search code examples
rlistlapplystandardized

Standardize and rescale each column of every element in a list


I have a list of 5 dataframes like so:

mydf <- data.frame(x=c(1:5), y=c(21:25),z=rnorm(1:5), p=rnorm(2:6), f=rnorm(3:7))
mylist <- rep(list(mydf),5)
names(mylist) <-c("2006-01-01","2006-01-02","2006-01-03","2006-01-04","2006-01-05") 

I also have a 3 step formula and following bits of code i put together:

enter image description here

Step 1 - the code for this is as follows this needs to be calculated for every row of the same column. if x is an element of "z" , "f" or "p" then:

z = x - mean(column))/sd(column)

2 - rescale z scores from 0 using values from step 1

rz = abs(min(z)) + z

3 - Rescale RZ scores from step 2 such that they lie between 0 and 1

mrz = rz/max(rz)

I need to apply this formula to columns "z", "p", "f" only also objective_col <- colnames(mylist$'2006-01-05'[,3:5]) in every element of mylist using apply , sapply, lapply or other type of loop:

it will probably look something like:

lapply(mylist, FUN = function(x) .......)

Outputs should be in the same layout and format as mydf all stored in mylist2 <- list()

I will continue to update this as i make more progress. I'm still learning how to use loops and functions..Thanks to anyone that can provide some input.


Solution

  • out <- lapply(mylist, function(x) {
      x[, c("z", "p", "f")] <- apply(x[, c("z", "p", "f")], 2, function(y) {
        y2 <- scale(y)
        return((y2 + abs(min(y2))) / max(y2))    
      })
    return(x)
    })