rapply to nested list of data frames in R

后端 未结 3 921
太阳男子
太阳男子 2020-12-20 14:48

i have a nested list whose fundamental element is data frames, and i want to traverse this list recursively to do some computation of each data frame, finally to get a neste

3条回答
  •  萌比男神i
    2020-12-20 15:42

    Update June 2020:

    You can now also use rrapply in the rrapply-package, (an extended version of base rapply). rrapply includes an additional argument dfaslist, which if set to FALSE does not treat data.frames as list-like objects by recursing into their individual columns:

    library(rrapply)
    
    L <- list(a = BOD, b = BOD)
    
    ## apply f to data.frames 
    rrapply(L, f = colSums, dfaslist = FALSE)
    #> $a
    #>   Time demand 
    #>     22     89 
    #> 
    #> $b
    #>   Time demand 
    #>     22     89
    
    ## apply f to individual columns of data.frames
    rrapply(L, f = function(x, .xname) if(.xname == "demand") scale(x) else x)
    #> $a
    #>   Time     demand
    #> 1    1 -1.4108974
    #> 2    2 -0.9789900
    #> 3    3  0.8998070
    #> 4    4  0.2519460
    #> 5    5  0.1655645
    #> 6    7  1.0725699
    #> 
    #> $b
    #>   Time     demand
    #> 1    1 -1.4108974
    #> 2    2 -0.9789900
    #> 3    3  0.8998070
    #> 4    4  0.2519460
    #> 5    5  0.1655645
    #> 6    7  1.0725699
    

提交回复
热议问题