Pooya Lalehzari
2014-Oct-07 16:55 UTC
[R] Conditional Data Manipulation -Cumulative Product
Hello, I have three datasets StartSignals, MainData, StopSignals and need to compound the data for each variable in MainData over dates that fall between the Start and Stop signals. (Stop signals are common and the same to all X1:X5 variables). Please see sample below: The one way I was thinking of doing this project was to setup a nested "FOR" loop and go through the three data matrices. Is there a more elegant way of doing this? Thank you. StartSignals: Date X1 X2 X3 X4 X5 1/1/2014 0 0 0 0 0 1/2/2014 0 1 0 0 1 1/3/2014 0 0 1 0 0 1/4/2014 0 0 0 0 0 1/5/2014 1 0 0 0 1 1/6/2014 0 0 1 0 0 1/7/2014 0 0 0 1 0 1/8/2014 0 0 0 0 0 1/9/2014 0 0 0 0 0 1/10/2014 0 0 1 1 0 1/11/2014 1 0 0 0 0 1/12/2014 0 1 0 0 1 1/13/2014 0 0 0 0 0 MainData: Date X1 X2 X3 X4 X5 1/1/2014 1.92 1.38 0.83 1.25 1.12 1/2/2014 0.67 1.51 1.21 0.06 1.24 1/3/2014 1.09 0.09 0.2 1.62 0.3 1/4/2014 1.81 1.33 1.57 1.68 1.41 1/5/2014 1.04 0.38 1.72 1.98 1.23 1/6/2014 1.69 1.12 0.76 1.45 1.99 1/7/2014 1.57 1.3 1.22 0.66 1.75 1/8/2014 0.5 1.75 0.27 0.09 1.91 1/9/2014 0 1.26 0.59 0.4 1.81 1/10/2014 1.31 1.57 1.68 0.98 1.79 1/11/2014 0.43 1.63 1.98 1.46 0.81 1/12/2014 1.51 0.78 1.63 0.46 1.84 1/13/2014 0.26 0.34 0.34 0.97 1.13 StopSignals: Date Stop 1/1/2014 0 1/2/2014 0 1/3/2014 1 1/4/2014 0 1/5/2014 1 1/6/2014 0 1/7/2014 0 1/8/2014 1 1/9/2014 0 1/10/2014 0 1/11/2014 0 1/12/2014 0 1/13/2014 1 ExpectedResult: Date X1 X2 X3 X4 X5 1/1/2014 0 0 0 0 0 1/2/2014 0 1.51 0 0 1.24 1/3/2014 0 0.14 0.2 0 0.37 1/4/2014 0 0 0 0 0 1/5/2014 1.04 0 0 0 1.23 1/6/2014 0 0 0.76 0 0 1/7/2014 0 0 0.93 0.66 0 1/8/2014 0 0 0.25 0.06 0 1/9/2014 0 0 0 0 0 1/10/2014 0 0 1.68 0.98 0 1/11/2014 0.43 0 3.33 1.43 0 1/12/2014 0.65 0.78 5.42 0.66 1.84 1/13/2014 0.17 0.27 1.84 0.64 2.08 *** We are pleased to announce that, as of October 20th, 2014, we will be moving to our new office at: Platinum Partners 250 West 55th Street, 14th Floor, New York, NY 10019 T: 212.582.2222 | F: 212.582.2424 *** THIS E-MAIL IS FOR THE SOLE USE OF THE INTENDED RECIPIENT(S) AND MAY CONTAIN CONFIDENTIAL AND PRIVILEGED INFORMATION.ANY UNAUTHORIZED REVIEW, USE, DISCLOSURE OR DISTRIBUTION IS PROHIBITED. IF YOU ARE NOT THE INTENDED RECIPIENT, PLEASE CONTACT THE SENDER BY REPLY E-MAIL AND DESTROY ALL COPIES OF THE ORIGINAL E-MAIL. [[alternative HTML version deleted]]
David L Carlson
2014-Oct-07 19:13 UTC
[R] Conditional Data Manipulation -Cumulative Product
You need to use plain text, not html in your email. Your data are scrambled (see below). It is better to send your data using the R dput() function: dput(StartSignals) dput(MainData) dput(StopSignals) ------------------------------------- David L Carlson Department of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Pooya Lalehzari Sent: Tuesday, October 7, 2014 11:55 AM To: R help Subject: [R] Conditional Data Manipulation -Cumulative Product Hello, I have three datasets StartSignals, MainData, StopSignals and need to compound the data for each variable in MainData over dates that fall between the Start and Stop signals. (Stop signals are common and the same to all X1:X5 variables). Please see sample below: The one way I was thinking of doing this project was to setup a nested "FOR" loop and go through the three data matrices. Is there a more elegant way of doing this? Thank you. StartSignals: Date X1 X2 X3 X4 X5 1/1/2014 0 0 0 0 0 1/2/2014 0 1 0 0 1 1/3/2014 0 0 1 0 0 1/4/2014 0 0 0 0 0 1/5/2014 1 0 0 0 1 1/6/2014 0 0 1 0 0 1/7/2014 0 0 0 1 0 1/8/2014 0 0 0 0 0 1/9/2014 0 0 0 0 0 1/10/2014 0 0 1 1 0 1/11/2014 1 0 0 0 0 1/12/2014 0 1 0 0 1 1/13/2014 0 0 0 0 0 MainData: Date X1 X2 X3 X4 X5 1/1/2014 1.92 1.38 0.83 1.25 1.12 1/2/2014 0.67 1.51 1.21 0.06 1.24 1/3/2014 1.09 0.09 0.2 1.62 0.3 1/4/2014 1.81 1.33 1.57 1.68 1.41 1/5/2014 1.04 0.38 1.72 1.98 1.23 1/6/2014 1.69 1.12 0.76 1.45 1.99 1/7/2014 1.57 1.3 1.22 0.66 1.75 1/8/2014 0.5 1.75 0.27 0.09 1.91 1/9/2014 0 1.26 0.59 0.4 1.81 1/10/2014 1.31 1.57 1.68 0.98 1.79 1/11/2014 0.43 1.63 1.98 1.46 0.81 1/12/2014 1.51 0.78 1.63 0.46 1.84 1/13/2014 0.26 0.34 0.34 0.97 1.13 StopSignals: Date Stop 1/1/2014 0 1/2/2014 0 1/3/2014 1 1/4/2014 0 1/5/2014 1 1/6/2014 0 1/7/2014 0 1/8/2014 1 1/9/2014 0 1/10/2014 0 1/11/2014 0 1/12/2014 0 1/13/2014 1 ExpectedResult: Date X1 X2 X3 X4 X5 1/1/2014 0 0 0 0 0 1/2/2014 0 1.51 0 0 1.24 1/3/2014 0 0.14 0.2 0 0.37 1/4/2014 0 0 0 0 0 1/5/2014 1.04 0 0 0 1.23 1/6/2014 0 0 0.76 0 0 1/7/2014 0 0 0.93 0.66 0 1/8/2014 0 0 0.25 0.06 0 1/9/2014 0 0 0 0 0 1/10/2014 0 0 1.68 0.98 0 1/11/2014 0.43 0 3.33 1.43 0 1/12/2014 0.65 0.78 5.42 0.66 1.84 1/13/2014 0.17 0.27 1.84 0.64 2.08 *** We are pleased to announce that, as of October 20th, 2014, we will be moving to our new office at: Platinum Partners 250 West 55th Street, 14th Floor, New York, NY 10019 T: 212.582.2222 | F: 212.582.2424 *** THIS E-MAIL IS FOR THE SOLE USE OF THE INTENDED RECIPIENT(S) AND MAY CONTAIN CONFIDENTIAL AND PRIVILEGED INFORMATION.ANY UNAUTHORIZED REVIEW, USE, DISCLOSURE OR DISTRIBUTION IS PROHIBITED. IF YOU ARE NOT THE INTENDED RECIPIENT, PLEASE CONTACT THE SENDER BY REPLY E-MAIL AND DESTROY ALL COPIES OF THE ORIGINAL E-MAIL. [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list 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.