【问题标题】:multiple transform on df with plyr使用 plyr 对 df 进行多重变换
【发布时间】:2010-12-24 11:16:48
【问题描述】:

我有一个 df,我想用 plyr 对其进行多次转换:

idplot / idtree / species /  condition / dbh_cm / h_m / hblc_m


CalcG <- function (df) transform(df, g_m2 = pi * (dbh_cm^2)/40000)

CalcHD <- function (df) transform(df, hd = h_m / dbh_cm)

...

可以在一个函数中完成吗? 非常感谢。

【问题讨论】:

    标签: r plyr


    【解决方案1】:

    是的,只需用逗号分隔您的作业:

    > head(swiss)
                 Fertility Agriculture Examination Education Catholic
    Courtelary        80.2        17.0          15        12     9.96
    Delemont          83.1        45.1           6         9    84.84
    Franches-Mnt      92.5        39.7           5         5    93.40
    Moutier           85.8        36.5          12         7    33.77
    Neuveville        76.9        43.5          17        15     5.16
    Porrentruy        76.1        35.3           9         7    90.57
                 Infant.Mortality
    Courtelary               22.2
    Delemont                 22.2
    Franches-Mnt             20.2
    Moutier                  20.3
    Neuveville               20.6
    Porrentruy               26.6
    
    > head(transform(swiss, Agriculture = Agriculture ^ 2, Catholic = -Catholic))
                 Fertility Agriculture Examination Education Catholic
    Courtelary        80.2      289.00          15        12    -9.96
    Delemont          83.1     2034.01           6         9   -84.84
    Franches-Mnt      92.5     1576.09           5         5   -93.40
    Moutier           85.8     1332.25          12         7   -33.77
    Neuveville        76.9     1892.25          17        15    -5.16
    Porrentruy        76.1     1246.09           9         7   -90.57
                 Infant.Mortality
    Courtelary               22.2
    Delemont                 22.2
    Franches-Mnt             20.2
    Moutier                  20.3
    Neuveville               20.6
    Porrentruy               26.6
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-28
      • 1970-01-01
      • 1970-01-01
      • 2015-01-27
      • 2013-06-13
      • 2015-03-21
      • 2021-11-25
      • 1970-01-01
      相关资源
      最近更新 更多