[R] How to create a new data.frame based on calculation of subsets of an existing data.frame

Ioanna Ioannou ||54250 @end|ng |rom m@n@com
Tue Dec 17 20:38:02 CET 2019


Hello everyone,


I have the following problem: I have a data.frame with multiple fields.

If I had to do my calculations for a given combination of IM.type and Taxonomy is the following:

D <- read.csv('Test_v2.csv')

names(D)

VC <- 0.01*( subset(D, IM.type == 'PGA' & Damage.state == 'DS1' & Taxonomy == 'ER+ETR_H1')[10:13] -

              subset(D, IM.type == 'PGA' & Damage.state == 'DS2' & Taxonomy == 'ER+ETR_H1')[10:13])  +

  0.02*(     subset(D, IM.type == 'PGA' & Damage.state == 'DS2' & Taxonomy == 'ER+ETR_H1')[10:13] -

              subset(D, IM.type == 'PGA' & Damage.state == 'DS3' & Taxonomy == 'ER+ETR_H1')[10:13])  +

  0.43*( subset(D, IM.type == 'PGA' & Damage.state == 'DS3' & Taxonomy == 'ER+ETR_H1')[10:13] -

           subset(D, IM.type == 'PGA' & Damage.state == 'DS4' & Taxonomy == 'ER+ETR_H1')[10:13])  +

  1.0*( subset(D, IM.type == 'PGA' & Damage.state == 'DS4' & Taxonomy == 'ER+ETR_H1')[10:13])

So the question is how can I do that in an automated way for all possible combinations and store the results in new data.frame  which would look like this:

Ref.No. Region  IM.type Taxonomy        IM_1   IM_2   IM_3   IM_4   VC_1   VC_2   VC_3   VC_4
1622   South America   PGA     ER+ETR_H1       1.00E-06       0.08   0.16   0.24     3.49e-294               3.449819e-05  0.002748889     0.01122911

Thanks in advance,

Best, ,

ioanna



More information about the R-help mailing list