# Applying a certain formula to a repeated sample data

7 messages
Open this post in threaded view
|

## Applying a certain formula to a repeated sample data

 Dear List, I have three data-column data. The data is of the form: 1 8590 12516 2 8641 98143 3 8705 98916 4 8750 89911 5 8685 104835 6 8629 121963 7 8676 77655 1 8577 81081 2 8593 83385 3 8642 112164 4 8708 103684 5 8622 83982 6 8593 75944 7 8600 97036 1 8650 104911 2 8730 114098 3 8731 99421 4 8715 85707 5 8717 81273 6 8739 106462 7 8684 110635 1 8713 105214 2 8771 92456 3 8759 109270 4 8762 99150 5 8730 77306 6 8780 86324 7 8804 90214 1 8797 99894 2 8863 95177 3 8873 95910 4 8827 108511 5 8806 115636 6 8869 85542 7 8854 111018 1 8571 93247 2 8533 85105 3 8553 114725 4 8561 122195 5 8532 100945 6 8560 108552 7 8634 108707 1 8646 117420 2 8633 113823 3 8680 82763 4 8765 121072 5 8756 89835 6 8750 104578 7 8790 88429 I wish to calculate average of the second and third columns based on the first column for each repeated 7 days. The length of the data is 1442. That is 206 by 7. So I should arrive at 207 data points for each of the two columns after calculating the mean of each group 1-7. I have both tried factor/tapply and aggregate functions but seem not to be making progress. Thank you very much for your idea. Best wishes Ogbos         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Applying a certain formula to a repeated sample data

 Hi Ogbos, If we assume that you have a 3 column data frame named oodf, how about: oodf[,4]<-floor((cumsum(oodf[,1])-1)/28) col2means<-by(oodf[,2],oodf[,4],mean) col3means<-by(oodf[,3],oodf[,4],mean) Jim On Wed, Nov 28, 2018 at 2:06 PM Ogbos Okike <[hidden email]> wrote: > > Dear List, > I have three data-column data. The data is of the form: > 1 8590 12516 > 2 8641 98143 > 3 8705 98916 > 4 8750 89911 > 5 8685 104835 > 6 8629 121963 > 7 8676 77655 > 1 8577 81081 > 2 8593 83385 > 3 8642 112164 > 4 8708 103684 > 5 8622 83982 > 6 8593 75944 > 7 8600 97036 > 1 8650 104911 > 2 8730 114098 > 3 8731 99421 > 4 8715 85707 > 5 8717 81273 > 6 8739 106462 > 7 8684 110635 > 1 8713 105214 > 2 8771 92456 > 3 8759 109270 > 4 8762 99150 > 5 8730 77306 > 6 8780 86324 > 7 8804 90214 > 1 8797 99894 > 2 8863 95177 > 3 8873 95910 > 4 8827 108511 > 5 8806 115636 > 6 8869 85542 > 7 8854 111018 > 1 8571 93247 > 2 8533 85105 > 3 8553 114725 > 4 8561 122195 > 5 8532 100945 > 6 8560 108552 > 7 8634 108707 > 1 8646 117420 > 2 8633 113823 > 3 8680 82763 > 4 8765 121072 > 5 8756 89835 > 6 8750 104578 > 7 8790 88429 > > I wish to calculate average of the second and third columns based on the > first column for each repeated 7 days. The length of the data is 1442. That > is 206 by 7. So I should arrive at 207 data points for each of the two > columns after calculating the mean of each group 1-7. > > I have both tried factor/tapply and aggregate functions but seem not to be > making progress. > > Thank you very much for your idea. > > Best wishes > Ogbos > >         [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Applying a certain formula to a repeated sample data: SOLVED

 On Wed, Nov 28, 2018 at 4:31 AM Jim Lemon <[hidden email]> wrote: > Hi Ogbos, > If we assume that you have a 3 column data frame named oodf, how about: > > Dear Jim, Thank you so much. The code just made life very easier for me. best regards Ogbos > oodf[,4]<-floor((cumsum(oodf[,1])-1)/28) > col2means<-by(oodf[,2],oodf[,4],mean) > col3means<-by(oodf[,3],oodf[,4],mean) > > Jim > > On Wed, Nov 28, 2018 at 2:06 PM Ogbos Okike <[hidden email]> > wrote: > > > > Dear List, > > I have three data-column data. The data is of the form: > > 1 8590 12516 > > 2 8641 98143 > > 3 8705 98916 > > 4 8750 89911 > > 5 8685 104835 > > 6 8629 121963 > > 7 8676 77655 > > 1 8577 81081 > > 2 8593 83385 > > 3 8642 112164 > > 4 8708 103684 > > 5 8622 83982 > > 6 8593 75944 > > 7 8600 97036 > > 1 8650 104911 > > 2 8730 114098 > > 3 8731 99421 > > 4 8715 85707 > > 5 8717 81273 > > 6 8739 106462 > > 7 8684 110635 > > 1 8713 105214 > > 2 8771 92456 > > 3 8759 109270 > > 4 8762 99150 > > 5 8730 77306 > > 6 8780 86324 > > 7 8804 90214 > > 1 8797 99894 > > 2 8863 95177 > > 3 8873 95910 > > 4 8827 108511 > > 5 8806 115636 > > 6 8869 85542 > > 7 8854 111018 > > 1 8571 93247 > > 2 8533 85105 > > 3 8553 114725 > > 4 8561 122195 > > 5 8532 100945 > > 6 8560 108552 > > 7 8634 108707 > > 1 8646 117420 > > 2 8633 113823 > > 3 8680 82763 > > 4 8765 121072 > > 5 8756 89835 > > 6 8750 104578 > > 7 8790 88429 > > > > I wish to calculate average of the second and third columns based on the > > first column for each repeated 7 days. The length of the data is 1442. > That > > is 206 by 7. So I should arrive at 207 data points for each of the two > > columns after calculating the mean of each group 1-7. > > > > I have both tried factor/tapply and aggregate functions but seem not to > be > > making progress. > > > > Thank you very much for your idea. > > > > Best wishes > > Ogbos > > > >         [[alternative HTML version deleted]] > > > > ______________________________________________ > > [hidden email] 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]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Applying a certain formula to a repeated sample data

 In reply to this post by Jim Lemon-4 Dear Jim, I wish also to use the means calculated and apply a certain formula on the same data frame. In particular, I would like to subtract the means of each of these seven days from each of the seven days and and divide the outcome by the same means. If I represent m1 by the means of each seven days in column 1, and c1 is taken as column 1 data. My formula will be of the form: aa<-(c1-m1)/m1. I tried it on the first 7 rows and I have what I am looking for.:  -0.0089986156   -0.0031149054    0.0042685741    0.0094600831    0.0019612367   -0.0044993078    0.0009229349 But doing it manually will take much time. Many thanks for going a step further to assist me. Warmest regards. Ogbos On Wed, Nov 28, 2018 at 4:31 AM Jim Lemon <[hidden email]> wrote: > Hi Ogbos, > If we assume that you have a 3 column data frame named oodf, how about: > > oodf[,4]<-floor((cumsum(oodf[,1])-1)/28) > col2means<-by(oodf[,2],oodf[,4],mean) > col3means<-by(oodf[,3],oodf[,4],mean) > > Jim > > On Wed, Nov 28, 2018 at 2:06 PM Ogbos Okike <[hidden email]> > wrote: > > > > Dear List, > > I have three data-column data. The data is of the form: > > 1 8590 12516 > > 2 8641 98143 > > 3 8705 98916 > > 4 8750 89911 > > 5 8685 104835 > > 6 8629 121963 > > 7 8676 77655 > > 1 8577 81081 > > 2 8593 83385 > > 3 8642 112164 > > 4 8708 103684 > > 5 8622 83982 > > 6 8593 75944 > > 7 8600 97036 > > 1 8650 104911 > > 2 8730 114098 > > 3 8731 99421 > > 4 8715 85707 > > 5 8717 81273 > > 6 8739 106462 > > 7 8684 110635 > > 1 8713 105214 > > 2 8771 92456 > > 3 8759 109270 > > 4 8762 99150 > > 5 8730 77306 > > 6 8780 86324 > > 7 8804 90214 > > 1 8797 99894 > > 2 8863 95177 > > 3 8873 95910 > > 4 8827 108511 > > 5 8806 115636 > > 6 8869 85542 > > 7 8854 111018 > > 1 8571 93247 > > 2 8533 85105 > > 3 8553 114725 > > 4 8561 122195 > > 5 8532 100945 > > 6 8560 108552 > > 7 8634 108707 > > 1 8646 117420 > > 2 8633 113823 > > 3 8680 82763 > > 4 8765 121072 > > 5 8756 89835 > > 6 8750 104578 > > 7 8790 88429 > > > > I wish to calculate average of the second and third columns based on the > > first column for each repeated 7 days. The length of the data is 1442. > That > > is 206 by 7. So I should arrive at 207 data points for each of the two > > columns after calculating the mean of each group 1-7. > > > > I have both tried factor/tapply and aggregate functions but seem not to > be > > making progress. > > > > Thank you very much for your idea. > > > > Best wishes > > Ogbos > > > >         [[alternative HTML version deleted]] > > > > ______________________________________________ > > [hidden email] 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]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|