Ioanna Ioannou
2019-Dec-17 19:38 UTC
[R] How to create a new data.frame based on calculation of subsets of an existing data.frame
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
Ioanna Ioannou
2019-Dec-17 19:43 UTC
[R] FW: How to create a new data.frame based on calculation of subsets of an existing data.frame
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 ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Ioanna Ioannou
2019-Dec-17 20:11 UTC
[R] How to create a new data.frame based on calculation of subsets of an existing data.frame
Just i case you cant see the data: Test.v2 <- data.frame(Ref.No = c(1622, 1623, 1624, 1625, 1626, 1627, 1628, 1629), IM.type = c('PGA', 'PGA', 'PGA', 'PGA', 'Sa', 'Sa', 'Sa', 'Sa'), Damage.state = c('DS1', 'DS2', 'DS3', 'DS4','DS1', 'DS2', 'DS3', 'DS4'), Taxonomy = c('ER+ETR_H1','ER+ETR_H1','ER+ETR_H1','ER+ETR_H1','ER+ETR_H2','ER+ETR_H2','ER+ETR_H2','ER+ETR_H2'), IM_1 = c(0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00), IM_2 = c(0.08, 0.08, 0.08, 0.08, 0.08, 0.08, 0.08, 0.08), IM_3 = c(0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16), IM_1 = c(0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24), Prob.of.exceedance_1 = c(0,0,0,0,0,0,0,0), Prob.of.exceedance_2 = c(0,0,0,0,0,0,0,0), Prob.of.exceedance_3 = c(0.26,0.001,0.00019,0.000000573,0.04,0.00017,0.000215,0.000472), Prob.of.exceedance_4 = c(0.72,0.03,0.008,0.000061,0.475,0.0007,0.00435,0.000405) ) ________________________________ From: R-help <r-help-bounces at r-project.org> on behalf of Ioanna Ioannou <ii54250 at msn.com> Sent: 17 December 2019 19:43 To: r-help at r-project.org <r-help at r-project.org> Subject: [R] FW: How to create a new data.frame based on calculation of subsets of an existing data.frame 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 ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]]
Ivan Krylov
2019-Dec-17 21:00 UTC
[R] How to create a new data.frame based on calculation of subsets of an existing data.frame
Hello Ioanna! Please don't post the same question 4 times in a row. One is just enough; you can see it posted successfully in the R-help archives: https://stat.ethz.ch/pipermail/r-help/2019-December/465108.html On Tue, 17 Dec 2019 19:38:02 +0000 Ioanna Ioannou <ii54250 at msn.com> wrote:> 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])If this is supposed to be VC_1 ... VC_4, one way to make a data.frame of it is: as.data.frame(as.list(setNames(VC, paste0('VC_', 1:4)))) (Though it's not the most elegant way, I'll have to admit.) Use cbind() to add more columns to the resulting data.frame. Note that this representation of the data might be not very effective to work with.> So the question is how can I do that in an automated way for all > possible combinationsWhat are "all possible combinations" here? -- Best regards, Ivan