FMM
FMM

Reputation: 2005

Apply approxfun in a nested list in R

I have a nested list as in the example below:

 # Nested list
    df <- list(
  list(
    Mean = seq(0,100, length.out = 1500),
    MVC = seq(0,100, length.out = 1400),
    z = seq(0,700, length.out = 1450)),
  list(
    Mean = seq(0,100, length.out = 1480),
    MVC = seq(0,100, length.out = 1460),
    z = seq(200,900, length.out = 1490)
  )
)

And I need to interpolate ONLY the Mean and MVC in the two lists, so that they are all the same length. In this case, I need to interpolate all of them to the max length (1500).

I am trying to do that using

lapply(df, function(x) approxfun(x)(seq(0,100,length.out = newlength)))

where newlength is equal to 1500. However, this is not working.

Any thoughts?

Upvotes: 1

Views: 241

Answers (1)

PKumar
PKumar

Reputation: 11128

Use rapply , since your data is lists of lists:

rapply(df, function(x) approxfun(x)(seq(0,100,length.out = newlength)) , how='list')

The above worked well on my system, since the result is too large to paste, I am unable to paste it.

Upvotes: 2

Related Questions