[R] Applying a certain formula to a repeated sample data

Ogbos Okike giftedlife2014 @ending from gm@il@com
Wed Nov 28 06:17:11 CET 2018


Dear Jim,

I don't think my problem is clear the way I put.

I have been trying to manually apply the formula to some rows.

This is what I have done.
I cut and past some rows from 1-7 and save each with a different file as
shown below:

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

Each of them are then read as:
d1<-read.table("dat1",col.names=c("n","CR","WW"))
d2<-read.table("dat2",col.names=c("n","CR","WW"))
d3<-read.table("dat3",col.names=c("n","CR","WW"))
d4<-read.table("dat4",col.names=c("n","CR","WW"))
d5<-read.table("dat5",col.names=c("n","CR","WW"))
d6<-read.table("dat6",col.names=c("n","CR","WW"))
d7<-read.table("dat7",col.names=c("n","CR","WW"))

And my formula for percentage change applied as follows for column 2:
a1<-((d1$CR-mean(d1$CR))/mean(CR))*100
a2<-((d2$CR-mean(d2$CR))/mean(CR))*100
a3<-((d3$CR-mean(d3$CR))/mean(CR))*100
a4<-((d4$CR-mean(d4$CR))/mean(CR))*100
a5<-((d5$CR-mean(d5$CR))/mean(CR))*100
a6<-((d6$CR-mean(d6$CR))/mean(CR))*100
a7<-((d7$CR-mean(d7$CR))/mean(CR))*100

a1-a7 actually gives percentage change in the data.

Instead of doing this one after the other, can you please give an
indication on how I may apply this formula to the data frame with probably
a code.

Thank you again.

Best
Ogbos

On Wed, Nov 28, 2018 at 5:15 AM Ogbos Okike <giftedlife2014 using gmail.com>
wrote:

> 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 <drjimlemon using gmail.com> 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 <giftedlife2014 using gmail.com>
>> 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]]
>> >
>> > ______________________________________________
>> > R-help using 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]]



More information about the R-help mailing list