Mutate multiple columns in a dataframe

后端 未结 6 941
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-29 06:01

I have a data set that looks like this.

bankname    bankid  year    totass  cash    bond    loans
Bank A      1       1881    244789  7250    20218   29513
B         


        
6条回答
  •  抹茶落季
    2020-11-29 06:41

    You might be making this a little harder than necessary. Just try this and see if it yields what you need.

    bankdata$CashtoAsset <- bankdata$cash / bankdata$totass
    bankdata$BondtoAsset <- bankdata$bond / bankdata$totass
    bankdata$loantoAsset <- bankdata$loans / bankdata$totass
    bankdata
    

    Yields this:

    bankname bankid year   totass   cash   bond loans CashtoAsset BondtoAsset loantoAsset 
    1   Bank A      1 1881   244789   7250  20218 29513  0.02961734 0.082593581  0.12056506 
    2   Bank B      2 1881   195755  10243 185151  2800  0.05232561 0.945830247  0.01430359 
    3   Bank C      3 1881   107736  13357 177612     0  0.12397899 1.648585431  0.00000 
    4   Bank D      4 1881   170600  35000  20000  5000  0.20515826 0.117233294  0.02930832 
    5   Bank E      5 1881 32000000 351266 314012     0  0.01097706 0.009812875  0.00000000
    

    This should get you started in the right direction.

提交回复
热议问题