Applying some functions to multiple objects

前端 未结 1 568
萌比男神i
萌比男神i 2021-01-06 18:15

I am on Mac OS 10.10 with R 3.1.1

Suppose I have the following data frames a and b with the same attributes:

a<- structu         


        
1条回答
  •  不要未来只要你来
    2021-01-06 19:19

    It is generally encouraged (by the R Core team memebers) that if you have several data sets that you want to simultaneously operate on, you should keep them all in a list. In order to achieve your goal, you can simply use mget and ls to retreive them from the global environment and then simply replace with first column multiplied by 2, e.g.,

    lapply(mget(ls(pattern = "[a-z]")), function(x) x <- x[1] * 2)
    # $a
    # X1
    # 1  2
    # 2  4
    # 3  6
    # 4  8
    # 5 10
    # 
    # $b
    # X1
    # 1 22
    # 2 24
    # 3 26
    # 4 28
    # 5 30
    

    0 讨论(0)
提交回复
热议问题