1

我有一个 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)

...

可以在一个功能中完成吗?非常感谢。

4

1 回答 1

4

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

> 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
于 2009-12-08T20:06:18.810 回答