tags:

views:

143

answers:

1

I have a df and I want to do multiple transform on it with 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)

...

Can be done in one function? Many thanks.

+2  A: 

Yes, just separate your assignments by commas:

> 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
Jonathan Chang