R: How to run a function/calculation on two lists based on their names?

前端 未结 1 816
无人及你
无人及你 2021-01-28 22:52

I want to run a function (in this case just a multiplication) on two list based on their names. Here some example data showing my structure:

A <- list(\"111\"         


        
相关标签:
1条回答
  • 2021-01-28 23:32

    You can use Map to loop over B and B names, this will also keep up the names:

    Map(function(x,y) A[[y]]*x, B, gsub('\\..*','',names(B)))
    
    #$`111.2000`
    #     [,1] [,2] [,3]
    #[1,]   36   18   54
    #[2,]   16   25    3
    #[3,]   56   10   20
    
    #$`112.2000`
    #     [,1] [,2] [,3]
    #[1,]   18   10   40
    #[2,]   35   18   60
    #[3,]    3   63   16
    
    #$`111.2001`
    #     [,1] [,2] [,3]
    #[1,]   81   30   18
    #[2,]   56   25    1
    #[3,]   28   20   16
    
    #$`112.2001`
    #     [,1] [,2] [,3]
    #[1,]   36   30   72
    #[2,]   30   30    6
    #[3,]    5   56    4
    
    0 讨论(0)
提交回复
热议问题