|
|
Hi R users,
I have a question about manipulating the data.
For example, there are several such data frames or matrices, and I want to
calculate the average value from all the data frames or matrices. How to do
it? Also, should I convert them to data frame or matrix first? Right now,
when I use typeof() function, each one is a list.
file1
jan feb mar apr may jun jul aug sep oct nov
app1 1.1 1.2 0.8 0.9 1.3 1.5 2.2 3.2 3.0 1.2 1.1
app2 3.1 3.2 2.8 2.5 2.3 2.5 3.2 3.0 2.9 1.8 1.8
app3 5.1 5.2 3.8 4.9 5.3 5.5 5.2 4.2 5.0 4.2 4.1
file2
jan feb mar apr may jun jul aug sep oct nov
app1 1.9 1.5 0.5 0.9 1.2 1.8 2.5 3.7 3.2 1.5 1.6
app2 3.5 3.7 2.3 2.2 2.5 2.0 3.6 3.2 2.8 1.2 1.4
app3 5.5 5.0 3.5 4.4 5.4 5.6 5.3 4.4 5.2 4.3 4.2
file3 has the similar structure and values...
There are eight such files, and when I use the function mean(file1, file2,
file3, ..., file8), it returns the error below. Thanks for your help.
Warning message:
In mean.default(file1, file2, file3, file4, file5, file6, file7, :
argument is not numeric or logical: returning NA
[[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.
|
|
Are you trying to take the mean over all cells, or over rows/columns within each dataframe. Also, are these different dataframes stored within a list or are they standalone?
-----Original Message-----
From: R-help [mailto: [hidden email]] On Behalf Of lily li
Sent: Tuesday, May 09, 2017 10:39 AM
To: R mailing list < [hidden email]>
Subject: [R] About calculating average values from several matrices
Hi R users,
I have a question about manipulating the data.
For example, there are several such data frames or matrices, and I want to calculate the average value from all the data frames or matrices. How to do it? Also, should I convert them to data frame or matrix first? Right now, when I use typeof() function, each one is a list.
file1
jan feb mar apr may jun jul aug sep oct nov
app1 1.1 1.2 0.8 0.9 1.3 1.5 2.2 3.2 3.0 1.2 1.1
app2 3.1 3.2 2.8 2.5 2.3 2.5 3.2 3.0 2.9 1.8 1.8
app3 5.1 5.2 3.8 4.9 5.3 5.5 5.2 4.2 5.0 4.2 4.1
file2
jan feb mar apr may jun jul aug sep oct nov
app1 1.9 1.5 0.5 0.9 1.2 1.8 2.5 3.7 3.2 1.5 1.6
app2 3.5 3.7 2.3 2.2 2.5 2.0 3.6 3.2 2.8 1.2 1.4
app3 5.5 5.0 3.5 4.4 5.4 5.6 5.3 4.4 5.2 4.3 4.2
file3 has the similar structure and values...
There are eight such files, and when I use the function mean(file1, file2, file3, ..., file8), it returns the error below. Thanks for your help.
Warning message:
In mean.default(file1, file2, file3, file4, file5, file6, file7, :
argument is not numeric or logical: returning NA
[[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.
______________________________________________
[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.
|
|
I'm trying to get a new dataframe or whatever to call, which has the same
structure with each file as listed above. For each cell in the new
dataframe or the new file, it is the average value from former dataframes
at the same location. Thanks.
On Tue, May 9, 2017 at 8:41 AM, Doran, Harold < [hidden email]> wrote:
> Are you trying to take the mean over all cells, or over rows/columns
> within each dataframe. Also, are these different dataframes stored within a
> list or are they standalone?
>
>
>
> -----Original Message-----
> From: R-help [mailto: [hidden email]] On Behalf Of lily li
> Sent: Tuesday, May 09, 2017 10:39 AM
> To: R mailing list < [hidden email]>
> Subject: [R] About calculating average values from several matrices
>
> Hi R users,
>
> I have a question about manipulating the data.
> For example, there are several such data frames or matrices, and I want to
> calculate the average value from all the data frames or matrices. How to do
> it? Also, should I convert them to data frame or matrix first? Right now,
> when I use typeof() function, each one is a list.
>
> file1
> jan feb mar apr may jun jul aug sep oct nov
>
> app1 1.1 1.2 0.8 0.9 1.3 1.5 2.2 3.2 3.0 1.2 1.1
> app2 3.1 3.2 2.8 2.5 2.3 2.5 3.2 3.0 2.9 1.8 1.8
> app3 5.1 5.2 3.8 4.9 5.3 5.5 5.2 4.2 5.0 4.2 4.1
>
> file2
> jan feb mar apr may jun jul aug sep oct nov
>
> app1 1.9 1.5 0.5 0.9 1.2 1.8 2.5 3.7 3.2 1.5 1.6
> app2 3.5 3.7 2.3 2.2 2.5 2.0 3.6 3.2 2.8 1.2 1.4
> app3 5.5 5.0 3.5 4.4 5.4 5.6 5.3 4.4 5.2 4.3 4.2
>
> file3 has the similar structure and values...
>
> There are eight such files, and when I use the function mean(file1, file2,
> file3, ..., file8), it returns the error below. Thanks for your help.
>
> Warning message:
> In mean.default(file1, file2, file3, file4, file5, file6, file7, :
> argument is not numeric or logical: returning NA
>
> [[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.
|
|
It’s not clear to me what your actual structure is. Can you provide str(object)? Assuming it is a list, and you want the mean over all cells or columns, you might want like this:
myData <- vector("list", 3)
for(i in 1:3){
myData[[i]] <- matrix(rnorm(100), 10, 10)
}
### mean over all cells
sapply(myData, function(x) mean(x))
### mean over all columns
sapply(myData, function(x) colMeans(x))
From: lily li [mailto: [hidden email]]
Sent: Tuesday, May 09, 2017 10:44 AM
To: Doran, Harold < [hidden email]>
Cc: R mailing list < [hidden email]>
Subject: Re: [R] About calculating average values from several matrices
I'm trying to get a new dataframe or whatever to call, which has the same structure with each file as listed above. For each cell in the new dataframe or the new file, it is the average value from former dataframes at the same location. Thanks.
On Tue, May 9, 2017 at 8:41 AM, Doran, Harold < [hidden email]<mailto: [hidden email]>> wrote:
Are you trying to take the mean over all cells, or over rows/columns within each dataframe. Also, are these different dataframes stored within a list or are they standalone?
-----Original Message-----
From: R-help [mailto: [hidden email]<mailto: [hidden email]>] On Behalf Of lily li
Sent: Tuesday, May 09, 2017 10:39 AM
To: R mailing list < [hidden email]<mailto: [hidden email]>>
Subject: [R] About calculating average values from several matrices
Hi R users,
I have a question about manipulating the data.
For example, there are several such data frames or matrices, and I want to calculate the average value from all the data frames or matrices. How to do it? Also, should I convert them to data frame or matrix first? Right now, when I use typeof() function, each one is a list.
file1
jan feb mar apr may jun jul aug sep oct nov
app1 1.1 1.2 0.8 0.9 1.3 1.5 2.2 3.2 3.0 1.2 1.1
app2 3.1 3.2 2.8 2.5 2.3 2.5 3.2 3.0 2.9 1.8 1.8
app3 5.1 5.2 3.8 4.9 5.3 5.5 5.2 4.2 5.0 4.2 4.1
file2
jan feb mar apr may jun jul aug sep oct nov
app1 1.9 1.5 0.5 0.9 1.2 1.8 2.5 3.7 3.2 1.5 1.6
app2 3.5 3.7 2.3 2.2 2.5 2.0 3.6 3.2 2.8 1.2 1.4
app3 5.5 5.0 3.5 4.4 5.4 5.6 5.3 4.4 5.2 4.3 4.2
file3 has the similar structure and values...
There are eight such files, and when I use the function mean(file1, file2, file3, ..., file8), it returns the error below. Thanks for your help.
Warning message:
In mean.default(file1, file2, file3, file4, file5, file6, file7, :
argument is not numeric or logical: returning NA
[[alternative HTML version deleted]]
______________________________________________
[hidden email]<mailto: [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.
[[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.
|
|
I meant for each cell, it takes the average from other dataframes at the
same cell. I don't know how to deal with row names and col names though, so
it has the error message.
On Tue, May 9, 2017 at 8:50 AM, Doran, Harold < [hidden email]> wrote:
> It’s not clear to me what your actual structure is. Can you provide
> str(object)? Assuming it is a list, and you want the mean over all cells or
> columns, you might want like this:
>
>
>
> myData <- vector("list", 3)
>
>
>
> for(i in 1:3){
>
> myData[[i]] <- matrix(rnorm(100), 10, 10)
>
> }
>
>
>
> ### mean over all cells
>
> sapply(myData, function(x) mean(x))
>
>
>
> ### mean over all columns
>
> sapply(myData, function(x) colMeans(x))
>
>
>
>
>
>
>
>
>
>
>
> *From:* lily li [mailto: [hidden email]]
> *Sent:* Tuesday, May 09, 2017 10:44 AM
> *To:* Doran, Harold < [hidden email]>
> *Cc:* R mailing list < [hidden email]>
> *Subject:* Re: [R] About calculating average values from several matrices
>
>
>
> I'm trying to get a new dataframe or whatever to call, which has the same
> structure with each file as listed above. For each cell in the new
> dataframe or the new file, it is the average value from former dataframes
> at the same location. Thanks.
>
>
>
> On Tue, May 9, 2017 at 8:41 AM, Doran, Harold < [hidden email]> wrote:
>
> Are you trying to take the mean over all cells, or over rows/columns
> within each dataframe. Also, are these different dataframes stored within a
> list or are they standalone?
>
>
>
>
> -----Original Message-----
> From: R-help [mailto: [hidden email]] On Behalf Of lily li
> Sent: Tuesday, May 09, 2017 10:39 AM
> To: R mailing list < [hidden email]>
> Subject: [R] About calculating average values from several matrices
>
> Hi R users,
>
> I have a question about manipulating the data.
> For example, there are several such data frames or matrices, and I want to
> calculate the average value from all the data frames or matrices. How to do
> it? Also, should I convert them to data frame or matrix first? Right now,
> when I use typeof() function, each one is a list.
>
> file1
> jan feb mar apr may jun jul aug sep oct nov
>
> app1 1.1 1.2 0.8 0.9 1.3 1.5 2.2 3.2 3.0 1.2 1.1
> app2 3.1 3.2 2.8 2.5 2.3 2.5 3.2 3.0 2.9 1.8 1.8
> app3 5.1 5.2 3.8 4.9 5.3 5.5 5.2 4.2 5.0 4.2 4.1
>
> file2
> jan feb mar apr may jun jul aug sep oct nov
>
> app1 1.9 1.5 0.5 0.9 1.2 1.8 2.5 3.7 3.2 1.5 1.6
> app2 3.5 3.7 2.3 2.2 2.5 2.0 3.6 3.2 2.8 1.2 1.4
> app3 5.5 5.0 3.5 4.4 5.4 5.6 5.3 4.4 5.2 4.3 4.2
>
> file3 has the similar structure and values...
>
> There are eight such files, and when I use the function mean(file1, file2,
> file3, ..., file8), it returns the error below. Thanks for your help.
>
> Warning message:
> In mean.default(file1, file2, file3, file4, file5, file6, file7, :
> argument is not numeric or logical: returning NA
>
> [[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.
|
|
If you want the mean of each element across you list of matrices the
following should provide what you are looking for where Reduce sums all
your matrix elements across matrices and the simply divided my the number
of matrices for the element-wise mean.
Reduce(`+`, mylist)/length(mylist)
Regards,
Charles
On Tue, May 9, 2017 at 9:52 AM, lily li < [hidden email]> wrote:
> I meant for each cell, it takes the average from other dataframes at the
> same cell. I don't know how to deal with row names and col names though, so
> it has the error message.
>
> On Tue, May 9, 2017 at 8:50 AM, Doran, Harold < [hidden email]> wrote:
>
> > It’s not clear to me what your actual structure is. Can you provide
> > str(object)? Assuming it is a list, and you want the mean over all cells
> or
> > columns, you might want like this:
> >
> >
> >
> > myData <- vector("list", 3)
> >
> >
> >
> > for(i in 1:3){
> >
> > myData[[i]] <- matrix(rnorm(100), 10, 10)
> >
> > }
> >
> >
> >
> > ### mean over all cells
> >
> > sapply(myData, function(x) mean(x))
> >
> >
> >
> > ### mean over all columns
> >
> > sapply(myData, function(x) colMeans(x))
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> > *From:* lily li [mailto: [hidden email]]
> > *Sent:* Tuesday, May 09, 2017 10:44 AM
> > *To:* Doran, Harold < [hidden email]>
> > *Cc:* R mailing list < [hidden email]>
> > *Subject:* Re: [R] About calculating average values from several matrices
> >
> >
> >
> > I'm trying to get a new dataframe or whatever to call, which has the same
> > structure with each file as listed above. For each cell in the new
> > dataframe or the new file, it is the average value from former dataframes
> > at the same location. Thanks.
> >
> >
> >
> > On Tue, May 9, 2017 at 8:41 AM, Doran, Harold < [hidden email]> wrote:
> >
> > Are you trying to take the mean over all cells, or over rows/columns
> > within each dataframe. Also, are these different dataframes stored
> within a
> > list or are they standalone?
> >
> >
> >
> >
> > -----Original Message-----
> > From: R-help [mailto: [hidden email]] On Behalf Of lily li
> > Sent: Tuesday, May 09, 2017 10:39 AM
> > To: R mailing list < [hidden email]>
> > Subject: [R] About calculating average values from several matrices
> >
> > Hi R users,
> >
> > I have a question about manipulating the data.
> > For example, there are several such data frames or matrices, and I want
> to
> > calculate the average value from all the data frames or matrices. How to
> do
> > it? Also, should I convert them to data frame or matrix first? Right now,
> > when I use typeof() function, each one is a list.
> >
> > file1
> > jan feb mar apr may jun jul aug sep oct
> nov
> >
> > app1 1.1 1.2 0.8 0.9 1.3 1.5 2.2 3.2 3.0 1.2
> 1.1
> > app2 3.1 3.2 2.8 2.5 2.3 2.5 3.2 3.0 2.9 1.8
> 1.8
> > app3 5.1 5.2 3.8 4.9 5.3 5.5 5.2 4.2 5.0 4.2
> 4.1
> >
> > file2
> > jan feb mar apr may jun jul aug sep oct
> nov
> >
> > app1 1.9 1.5 0.5 0.9 1.2 1.8 2.5 3.7 3.2 1.5
> 1.6
> > app2 3.5 3.7 2.3 2.2 2.5 2.0 3.6 3.2 2.8 1.2
> 1.4
> > app3 5.5 5.0 3.5 4.4 5.4 5.6 5.3 4.4 5.2 4.3
> 4.2
> >
> > file3 has the similar structure and values...
> >
> > There are eight such files, and when I use the function mean(file1,
> file2,
> > file3, ..., file8), it returns the error below. Thanks for your help.
> >
> > Warning message:
> > In mean.default(file1, file2, file3, file4, file5, file6, file7, :
> > argument is not numeric or logical: returning NA
> >
> > [[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-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.
|
|
Thanks very much, it works. But how to round the values to have only 1
decimal digit or 2 decimal digits? I think by dividing, the values are
double type now. Thanks again.
On Tue, May 9, 2017 at 9:04 AM, Charles Determan < [hidden email]>
wrote:
> If you want the mean of each element across you list of matrices the
> following should provide what you are looking for where Reduce sums all
> your matrix elements across matrices and the simply divided my the number
> of matrices for the element-wise mean.
>
> Reduce(`+`, mylist)/length(mylist)
>
> Regards,
> Charles
>
> On Tue, May 9, 2017 at 9:52 AM, lily li < [hidden email]> wrote:
>
>> I meant for each cell, it takes the average from other dataframes at the
>> same cell. I don't know how to deal with row names and col names though,
>> so
>> it has the error message.
>>
>> On Tue, May 9, 2017 at 8:50 AM, Doran, Harold < [hidden email]> wrote:
>>
>> > It’s not clear to me what your actual structure is. Can you provide
>> > str(object)? Assuming it is a list, and you want the mean over all
>> cells or
>> > columns, you might want like this:
>> >
>> >
>> >
>> > myData <- vector("list", 3)
>> >
>> >
>> >
>> > for(i in 1:3){
>> >
>> > myData[[i]] <- matrix(rnorm(100), 10, 10)
>> >
>> > }
>> >
>> >
>> >
>> > ### mean over all cells
>> >
>> > sapply(myData, function(x) mean(x))
>> >
>> >
>> >
>> > ### mean over all columns
>> >
>> > sapply(myData, function(x) colMeans(x))
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> > *From:* lily li [mailto: [hidden email]]
>> > *Sent:* Tuesday, May 09, 2017 10:44 AM
>> > *To:* Doran, Harold < [hidden email]>
>> > *Cc:* R mailing list < [hidden email]>
>> > *Subject:* Re: [R] About calculating average values from several
>> matrices
>>
>> >
>> >
>> >
>> > I'm trying to get a new dataframe or whatever to call, which has the
>> same
>> > structure with each file as listed above. For each cell in the new
>> > dataframe or the new file, it is the average value from former
>> dataframes
>> > at the same location. Thanks.
>> >
>> >
>> >
>> > On Tue, May 9, 2017 at 8:41 AM, Doran, Harold < [hidden email]> wrote:
>> >
>> > Are you trying to take the mean over all cells, or over rows/columns
>> > within each dataframe. Also, are these different dataframes stored
>> within a
>> > list or are they standalone?
>> >
>> >
>> >
>> >
>> > -----Original Message-----
>> > From: R-help [mailto: [hidden email]] On Behalf Of lily li
>> > Sent: Tuesday, May 09, 2017 10:39 AM
>> > To: R mailing list < [hidden email]>
>> > Subject: [R] About calculating average values from several matrices
>> >
>> > Hi R users,
>> >
>> > I have a question about manipulating the data.
>> > For example, there are several such data frames or matrices, and I want
>> to
>> > calculate the average value from all the data frames or matrices. How
>> to do
>> > it? Also, should I convert them to data frame or matrix first? Right
>> now,
>> > when I use typeof() function, each one is a list.
>> >
>> > file1
>> > jan feb mar apr may jun jul aug sep oct
>> nov
>> >
>> > app1 1.1 1.2 0.8 0.9 1.3 1.5 2.2 3.2 3.0 1.2
>> 1.1
>> > app2 3.1 3.2 2.8 2.5 2.3 2.5 3.2 3.0 2.9 1.8
>> 1.8
>> > app3 5.1 5.2 3.8 4.9 5.3 5.5 5.2 4.2 5.0 4.2
>> 4.1
>> >
>> > file2
>> > jan feb mar apr may jun jul aug sep oct
>> nov
>> >
>> > app1 1.9 1.5 0.5 0.9 1.2 1.8 2.5 3.7 3.2 1.5
>> 1.6
>> > app2 3.5 3.7 2.3 2.2 2.5 2.0 3.6 3.2 2.8 1.2
>> 1.4
>> > app3 5.5 5.0 3.5 4.4 5.4 5.6 5.3 4.4 5.2 4.3
>> 4.2
>> >
>> > file3 has the similar structure and values...
>> >
>> > There are eight such files, and when I use the function mean(file1,
>> file2,
>> > file3, ..., file8), it returns the error below. Thanks for your help.
>> >
>> > Warning message:
>> > In mean.default(file1, file2, file3, file4, file5, file6, file7, :
>> > argument is not numeric or logical: returning NA
>> >
>> > [[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-help>> PLEASE do read the posting guide http://www.R-project.org/posti>> ng-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.
|
|
Just call 'round' on your results then at your desired number of digits.
On Tue, May 9, 2017 at 10:09 AM, lily li < [hidden email]> wrote:
> Thanks very much, it works. But how to round the values to have only 1
> decimal digit or 2 decimal digits? I think by dividing, the values are
> double type now. Thanks again.
>
>
> On Tue, May 9, 2017 at 9:04 AM, Charles Determan < [hidden email]>
> wrote:
>
>> If you want the mean of each element across you list of matrices the
>> following should provide what you are looking for where Reduce sums all
>> your matrix elements across matrices and the simply divided my the number
>> of matrices for the element-wise mean.
>>
>> Reduce(`+`, mylist)/length(mylist)
>>
>> Regards,
>> Charles
>>
>> On Tue, May 9, 2017 at 9:52 AM, lily li < [hidden email]> wrote:
>>
>>> I meant for each cell, it takes the average from other dataframes at the
>>> same cell. I don't know how to deal with row names and col names though,
>>> so
>>> it has the error message.
>>>
>>> On Tue, May 9, 2017 at 8:50 AM, Doran, Harold < [hidden email]> wrote:
>>>
>>> > It’s not clear to me what your actual structure is. Can you provide
>>> > str(object)? Assuming it is a list, and you want the mean over all
>>> cells or
>>> > columns, you might want like this:
>>> >
>>> >
>>> >
>>> > myData <- vector("list", 3)
>>> >
>>> >
>>> >
>>> > for(i in 1:3){
>>> >
>>> > myData[[i]] <- matrix(rnorm(100), 10, 10)
>>> >
>>> > }
>>> >
>>> >
>>> >
>>> > ### mean over all cells
>>> >
>>> > sapply(myData, function(x) mean(x))
>>> >
>>> >
>>> >
>>> > ### mean over all columns
>>> >
>>> > sapply(myData, function(x) colMeans(x))
>>> >
>>> >
>>> >
>>> >
>>> >
>>> >
>>> >
>>> >
>>> >
>>> >
>>> >
>>> > *From:* lily li [mailto: [hidden email]]
>>> > *Sent:* Tuesday, May 09, 2017 10:44 AM
>>> > *To:* Doran, Harold < [hidden email]>
>>> > *Cc:* R mailing list < [hidden email]>
>>> > *Subject:* Re: [R] About calculating average values from several
>>> matrices
>>>
>>> >
>>> >
>>> >
>>> > I'm trying to get a new dataframe or whatever to call, which has the
>>> same
>>> > structure with each file as listed above. For each cell in the new
>>> > dataframe or the new file, it is the average value from former
>>> dataframes
>>> > at the same location. Thanks.
>>> >
>>> >
>>> >
>>> > On Tue, May 9, 2017 at 8:41 AM, Doran, Harold < [hidden email]> wrote:
>>> >
>>> > Are you trying to take the mean over all cells, or over rows/columns
>>> > within each dataframe. Also, are these different dataframes stored
>>> within a
>>> > list or are they standalone?
>>> >
>>> >
>>> >
>>> >
>>> > -----Original Message-----
>>> > From: R-help [mailto: [hidden email]] On Behalf Of lily
>>> li
>>> > Sent: Tuesday, May 09, 2017 10:39 AM
>>> > To: R mailing list < [hidden email]>
>>> > Subject: [R] About calculating average values from several matrices
>>> >
>>> > Hi R users,
>>> >
>>> > I have a question about manipulating the data.
>>> > For example, there are several such data frames or matrices, and I
>>> want to
>>> > calculate the average value from all the data frames or matrices. How
>>> to do
>>> > it? Also, should I convert them to data frame or matrix first? Right
>>> now,
>>> > when I use typeof() function, each one is a list.
>>> >
>>> > file1
>>> > jan feb mar apr may jun jul aug sep oct
>>> nov
>>> >
>>> > app1 1.1 1.2 0.8 0.9 1.3 1.5 2.2 3.2 3.0 1.2
>>> 1.1
>>> > app2 3.1 3.2 2.8 2.5 2.3 2.5 3.2 3.0 2.9 1.8
>>> 1.8
>>> > app3 5.1 5.2 3.8 4.9 5.3 5.5 5.2 4.2 5.0 4.2
>>> 4.1
>>> >
>>> > file2
>>> > jan feb mar apr may jun jul aug sep oct
>>> nov
>>> >
>>> > app1 1.9 1.5 0.5 0.9 1.2 1.8 2.5 3.7 3.2 1.5
>>> 1.6
>>> > app2 3.5 3.7 2.3 2.2 2.5 2.0 3.6 3.2 2.8 1.2
>>> 1.4
>>> > app3 5.5 5.0 3.5 4.4 5.4 5.6 5.3 4.4 5.2 4.3
>>> 4.2
>>> >
>>> > file3 has the similar structure and values...
>>> >
>>> > There are eight such files, and when I use the function mean(file1,
>>> file2,
>>> > file3, ..., file8), it returns the error below. Thanks for your help.
>>> >
>>> > Warning message:
>>> > In mean.default(file1, file2, file3, file4, file5, file6, file7, :
>>> > argument is not numeric or logical: returning NA
>>> >
>>> > [[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-help>>> PLEASE do read the posting guide http://www.R-project.org/posti>>> ng-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.
|
|
?round
From: lily li [mailto: [hidden email]]
Sent: Tuesday, May 09, 2017 11:10 AM
To: Charles Determan < [hidden email]>
Cc: Doran, Harold < [hidden email]>; R mailing list < [hidden email]>
Subject: Re: [R] About calculating average values from several matrices
Thanks very much, it works. But how to round the values to have only 1 decimal digit or 2 decimal digits? I think by dividing, the values are double type now. Thanks again.
On Tue, May 9, 2017 at 9:04 AM, Charles Determan < [hidden email]<mailto: [hidden email]>> wrote:
If you want the mean of each element across you list of matrices the following should provide what you are looking for where Reduce sums all your matrix elements across matrices and the simply divided my the number of matrices for the element-wise mean.
Reduce(`+`, mylist)/length(mylist)
Regards,
Charles
On Tue, May 9, 2017 at 9:52 AM, lily li < [hidden email]<mailto: [hidden email]>> wrote:
I meant for each cell, it takes the average from other dataframes at the
same cell. I don't know how to deal with row names and col names though, so
it has the error message.
On Tue, May 9, 2017 at 8:50 AM, Doran, Harold < [hidden email]<mailto: [hidden email]>> wrote:
> It’s not clear to me what your actual structure is. Can you provide
> str(object)? Assuming it is a list, and you want the mean over all cells or
> columns, you might want like this:
>
>
>
> myData <- vector("list", 3)
>
>
>
> for(i in 1:3){
>
> myData[[i]] <- matrix(rnorm(100), 10, 10)
>
> }
>
>
>
> ### mean over all cells
>
> sapply(myData, function(x) mean(x))
>
>
>
> ### mean over all columns
>
> sapply(myData, function(x) colMeans(x))
>
>
>
>
>
>
>
>
>
>
>
> *From:* lily li [mailto: [hidden email]<mailto: [hidden email]>]
> *Sent:* Tuesday, May 09, 2017 10:44 AM
> *To:* Doran, Harold < [hidden email]<mailto: [hidden email]>>
> *Cc:* R mailing list < [hidden email]<mailto: [hidden email]>>
> *Subject:* Re: [R] About calculating average values from several matrices
>
>
>
> I'm trying to get a new dataframe or whatever to call, which has the same
> structure with each file as listed above. For each cell in the new
> dataframe or the new file, it is the average value from former dataframes
> at the same location. Thanks.
>
>
>
> On Tue, May 9, 2017 at 8:41 AM, Doran, Harold < [hidden email]<mailto: [hidden email]>> wrote:
>
> Are you trying to take the mean over all cells, or over rows/columns
> within each dataframe. Also, are these different dataframes stored within a
> list or are they standalone?
>
>
>
>
> -----Original Message-----
> From: R-help [mailto: [hidden email]<mailto: [hidden email]>] On Behalf Of lily li
> Sent: Tuesday, May 09, 2017 10:39 AM
> To: R mailing list < [hidden email]<mailto: [hidden email]>>
> Subject: [R] About calculating average values from several matrices
>
> Hi R users,
>
> I have a question about manipulating the data.
> For example, there are several such data frames or matrices, and I want to
> calculate the average value from all the data frames or matrices. How to do
> it? Also, should I convert them to data frame or matrix first? Right now,
> when I use typeof() function, each one is a list.
>
> file1
> jan feb mar apr may jun jul aug sep oct nov
>
> app1 1.1 1.2 0.8 0.9 1.3 1.5 2.2 3.2 3.0 1.2 1.1
> app2 3.1 3.2 2.8 2.5 2.3 2.5 3.2 3.0 2.9 1.8 1.8
> app3 5.1 5.2 3.8 4.9 5.3 5.5 5.2 4.2 5.0 4.2 4.1
>
> file2
> jan feb mar apr may jun jul aug sep oct nov
>
> app1 1.9 1.5 0.5 0.9 1.2 1.8 2.5 3.7 3.2 1.5 1.6
> app2 3.5 3.7 2.3 2.2 2.5 2.0 3.6 3.2 2.8 1.2 1.4
> app3 5.5 5.0 3.5 4.4 5.4 5.6 5.3 4.4 5.2 4.3 4.2
>
> file3 has the similar structure and values...
>
> There are eight such files, and when I use the function mean(file1, file2,
> file3, ..., file8), it returns the error below. Thanks for your help.
>
> Warning message:
> In mean.default(file1, file2, file3, file4, file5, file6, file7, :
> argument is not numeric or logical: returning NA
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email]<mailto: [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]<mailto: [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.
[[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.
|
|
Yes, that means to control decimal numbers. For example, use round(2.3122,
digits=1), it gets 2.3
On Tue, May 9, 2017 at 9:11 AM, Doran, Harold < [hidden email]> wrote:
> ?round
>
>
>
>
>
> *From:* lily li [mailto: [hidden email]]
> *Sent:* Tuesday, May 09, 2017 11:10 AM
> *To:* Charles Determan < [hidden email]>
> *Cc:* Doran, Harold < [hidden email]>; R mailing list < [hidden email]
> >
> *Subject:* Re: [R] About calculating average values from several matrices
>
>
>
> Thanks very much, it works. But how to round the values to have only 1
> decimal digit or 2 decimal digits? I think by dividing, the values are
> double type now. Thanks again.
>
>
>
>
>
> On Tue, May 9, 2017 at 9:04 AM, Charles Determan < [hidden email]>
> wrote:
>
> If you want the mean of each element across you list of matrices the
> following should provide what you are looking for where Reduce sums all
> your matrix elements across matrices and the simply divided my the number
> of matrices for the element-wise mean.
>
> Reduce(`+`, mylist)/length(mylist)
>
> Regards,
>
> Charles
>
>
>
> On Tue, May 9, 2017 at 9:52 AM, lily li < [hidden email]> wrote:
>
> I meant for each cell, it takes the average from other dataframes at the
> same cell. I don't know how to deal with row names and col names though, so
> it has the error message.
>
> On Tue, May 9, 2017 at 8:50 AM, Doran, Harold < [hidden email]> wrote:
>
> > It’s not clear to me what your actual structure is. Can you provide
> > str(object)? Assuming it is a list, and you want the mean over all cells
> or
> > columns, you might want like this:
> >
> >
> >
> > myData <- vector("list", 3)
> >
> >
> >
> > for(i in 1:3){
> >
> > myData[[i]] <- matrix(rnorm(100), 10, 10)
> >
> > }
> >
> >
> >
> > ### mean over all cells
> >
> > sapply(myData, function(x) mean(x))
> >
> >
> >
> > ### mean over all columns
> >
> > sapply(myData, function(x) colMeans(x))
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> > *From:* lily li [mailto: [hidden email]]
> > *Sent:* Tuesday, May 09, 2017 10:44 AM
> > *To:* Doran, Harold < [hidden email]>
> > *Cc:* R mailing list < [hidden email]>
> > *Subject:* Re: [R] About calculating average values from several matrices
>
>
> >
> >
> >
> > I'm trying to get a new dataframe or whatever to call, which has the same
> > structure with each file as listed above. For each cell in the new
> > dataframe or the new file, it is the average value from former dataframes
> > at the same location. Thanks.
> >
> >
> >
> > On Tue, May 9, 2017 at 8:41 AM, Doran, Harold < [hidden email]> wrote:
> >
> > Are you trying to take the mean over all cells, or over rows/columns
> > within each dataframe. Also, are these different dataframes stored
> within a
> > list or are they standalone?
> >
> >
> >
> >
> > -----Original Message-----
> > From: R-help [mailto: [hidden email]] On Behalf Of lily li
> > Sent: Tuesday, May 09, 2017 10:39 AM
> > To: R mailing list < [hidden email]>
>
> > Subject: [R] About calculating average values from several matrices
> >
> > Hi R users,
> >
> > I have a question about manipulating the data.
> > For example, there are several such data frames or matrices, and I want
> to
> > calculate the average value from all the data frames or matrices. How to
> do
> > it? Also, should I convert them to data frame or matrix first? Right now,
> > when I use typeof() function, each one is a list.
> >
> > file1
> > jan feb mar apr may jun jul aug sep oct
> nov
> >
> > app1 1.1 1.2 0.8 0.9 1.3 1.5 2.2 3.2 3.0 1.2
> 1.1
> > app2 3.1 3.2 2.8 2.5 2.3 2.5 3.2 3.0 2.9 1.8
> 1.8
> > app3 5.1 5.2 3.8 4.9 5.3 5.5 5.2 4.2 5.0 4.2
> 4.1
> >
> > file2
> > jan feb mar apr may jun jul aug sep oct
> nov
> >
> > app1 1.9 1.5 0.5 0.9 1.2 1.8 2.5 3.7 3.2 1.5
> 1.6
> > app2 3.5 3.7 2.3 2.2 2.5 2.0 3.6 3.2 2.8 1.2
> 1.4
> > app3 5.5 5.0 3.5 4.4 5.4 5.6 5.3 4.4 5.2 4.3
> 4.2
> >
> > file3 has the similar structure and values...
> >
> > There are eight such files, and when I use the function mean(file1,
> file2,
> > file3, ..., file8), it returns the error below. Thanks for your help.
> >
> > Warning message:
> > In mean.default(file1, file2, file3, file4, file5, file6, file7, :
> > argument is not numeric or logical: returning NA
> >
> > [[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-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.
|
|
Im not sure if you’re asking a question or confirming that it works for you. But, obviously, the code below behaves as expected
From: lily li [mailto: [hidden email]]
Sent: Tuesday, May 09, 2017 11:13 AM
To: Doran, Harold < [hidden email]>
Cc: Charles Determan < [hidden email]>; R mailing list < [hidden email]>
Subject: Re: [R] About calculating average values from several matrices
Yes, that means to control decimal numbers. For example, use round(2.3122, digits=1), it gets 2.3
On Tue, May 9, 2017 at 9:11 AM, Doran, Harold < [hidden email]<mailto: [hidden email]>> wrote:
?round
From: lily li [mailto: [hidden email]<mailto: [hidden email]>]
Sent: Tuesday, May 09, 2017 11:10 AM
To: Charles Determan < [hidden email]<mailto: [hidden email]>>
Cc: Doran, Harold < [hidden email]<mailto: [hidden email]>>; R mailing list < [hidden email]<mailto: [hidden email]>>
Subject: Re: [R] About calculating average values from several matrices
Thanks very much, it works. But how to round the values to have only 1 decimal digit or 2 decimal digits? I think by dividing, the values are double type now. Thanks again.
On Tue, May 9, 2017 at 9:04 AM, Charles Determan < [hidden email]<mailto: [hidden email]>> wrote:
If you want the mean of each element across you list of matrices the following should provide what you are looking for where Reduce sums all your matrix elements across matrices and the simply divided my the number of matrices for the element-wise mean.
Reduce(`+`, mylist)/length(mylist)
Regards,
Charles
On Tue, May 9, 2017 at 9:52 AM, lily li < [hidden email]<mailto: [hidden email]>> wrote:
I meant for each cell, it takes the average from other dataframes at the
same cell. I don't know how to deal with row names and col names though, so
it has the error message.
On Tue, May 9, 2017 at 8:50 AM, Doran, Harold < [hidden email]<mailto: [hidden email]>> wrote:
> It’s not clear to me what your actual structure is. Can you provide
> str(object)? Assuming it is a list, and you want the mean over all cells or
> columns, you might want like this:
>
>
>
> myData <- vector("list", 3)
>
>
>
> for(i in 1:3){
>
> myData[[i]] <- matrix(rnorm(100), 10, 10)
>
> }
>
>
>
> ### mean over all cells
>
> sapply(myData, function(x) mean(x))
>
>
>
> ### mean over all columns
>
> sapply(myData, function(x) colMeans(x))
>
>
>
>
>
>
>
>
>
>
>
> *From:* lily li [mailto: [hidden email]<mailto: [hidden email]>]
> *Sent:* Tuesday, May 09, 2017 10:44 AM
> *To:* Doran, Harold < [hidden email]<mailto: [hidden email]>>
> *Cc:* R mailing list < [hidden email]<mailto: [hidden email]>>
> *Subject:* Re: [R] About calculating average values from several matrices
>
>
>
> I'm trying to get a new dataframe or whatever to call, which has the same
> structure with each file as listed above. For each cell in the new
> dataframe or the new file, it is the average value from former dataframes
> at the same location. Thanks.
>
>
>
> On Tue, May 9, 2017 at 8:41 AM, Doran, Harold < [hidden email]<mailto: [hidden email]>> wrote:
>
> Are you trying to take the mean over all cells, or over rows/columns
> within each dataframe. Also, are these different dataframes stored within a
> list or are they standalone?
>
>
>
>
> -----Original Message-----
> From: R-help [mailto: [hidden email]<mailto: [hidden email]>] On Behalf Of lily li
> Sent: Tuesday, May 09, 2017 10:39 AM
> To: R mailing list < [hidden email]<mailto: [hidden email]>>
> Subject: [R] About calculating average values from several matrices
>
> Hi R users,
>
> I have a question about manipulating the data.
> For example, there are several such data frames or matrices, and I want to
> calculate the average value from all the data frames or matrices. How to do
> it? Also, should I convert them to data frame or matrix first? Right now,
> when I use typeof() function, each one is a list.
>
> file1
> jan feb mar apr may jun jul aug sep oct nov
>
> app1 1.1 1.2 0.8 0.9 1.3 1.5 2.2 3.2 3.0 1.2 1.1
> app2 3.1 3.2 2.8 2.5 2.3 2.5 3.2 3.0 2.9 1.8 1.8
> app3 5.1 5.2 3.8 4.9 5.3 5.5 5.2 4.2 5.0 4.2 4.1
>
> file2
> jan feb mar apr may jun jul aug sep oct nov
>
> app1 1.9 1.5 0.5 0.9 1.2 1.8 2.5 3.7 3.2 1.5 1.6
> app2 3.5 3.7 2.3 2.2 2.5 2.0 3.6 3.2 2.8 1.2 1.4
> app3 5.5 5.0 3.5 4.4 5.4 5.6 5.3 4.4 5.2 4.3 4.2
>
> file3 has the similar structure and values...
>
> There are eight such files, and when I use the function mean(file1, file2,
> file3, ..., file8), it returns the error below. Thanks for your help.
>
> Warning message:
> In mean.default(file1, file2, file3, file4, file5, file6, file7, :
> argument is not numeric or logical: returning NA
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email]<mailto: [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]<mailto: [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.
[[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.
|
|
yes, I just tried for the dataframe and it works, so there is no problem on
this side.
On Tue, May 9, 2017 at 9:14 AM, Doran, Harold < [hidden email]> wrote:
> Im not sure if you’re asking a question or confirming that it works for
> you. But, obviously, the code below behaves as expected
>
>
>
> *From:* lily li [mailto: [hidden email]]
> *Sent:* Tuesday, May 09, 2017 11:13 AM
> *To:* Doran, Harold < [hidden email]>
> *Cc:* Charles Determan < [hidden email]>; R mailing list <
> [hidden email]>
>
> *Subject:* Re: [R] About calculating average values from several matrices
>
>
>
> Yes, that means to control decimal numbers. For example, use round(2.3122,
> digits=1), it gets 2.3
>
>
>
> On Tue, May 9, 2017 at 9:11 AM, Doran, Harold < [hidden email]> wrote:
>
> ?round
>
>
>
>
>
> *From:* lily li [mailto: [hidden email]]
> *Sent:* Tuesday, May 09, 2017 11:10 AM
> *To:* Charles Determan < [hidden email]>
> *Cc:* Doran, Harold < [hidden email]>; R mailing list < [hidden email]
> >
> *Subject:* Re: [R] About calculating average values from several matrices
>
>
>
> Thanks very much, it works. But how to round the values to have only 1
> decimal digit or 2 decimal digits? I think by dividing, the values are
> double type now. Thanks again.
>
>
>
>
>
> On Tue, May 9, 2017 at 9:04 AM, Charles Determan < [hidden email]>
> wrote:
>
> If you want the mean of each element across you list of matrices the
> following should provide what you are looking for where Reduce sums all
> your matrix elements across matrices and the simply divided my the number
> of matrices for the element-wise mean.
>
> Reduce(`+`, mylist)/length(mylist)
>
> Regards,
>
> Charles
>
>
>
> On Tue, May 9, 2017 at 9:52 AM, lily li < [hidden email]> wrote:
>
> I meant for each cell, it takes the average from other dataframes at the
> same cell. I don't know how to deal with row names and col names though, so
> it has the error message.
>
> On Tue, May 9, 2017 at 8:50 AM, Doran, Harold < [hidden email]> wrote:
>
> > It’s not clear to me what your actual structure is. Can you provide
> > str(object)? Assuming it is a list, and you want the mean over all cells
> or
> > columns, you might want like this:
> >
> >
> >
> > myData <- vector("list", 3)
> >
> >
> >
> > for(i in 1:3){
> >
> > myData[[i]] <- matrix(rnorm(100), 10, 10)
> >
> > }
> >
> >
> >
> > ### mean over all cells
> >
> > sapply(myData, function(x) mean(x))
> >
> >
> >
> > ### mean over all columns
> >
> > sapply(myData, function(x) colMeans(x))
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> > *From:* lily li [mailto: [hidden email]]
> > *Sent:* Tuesday, May 09, 2017 10:44 AM
> > *To:* Doran, Harold < [hidden email]>
> > *Cc:* R mailing list < [hidden email]>
> > *Subject:* Re: [R] About calculating average values from several matrices
>
>
> >
> >
> >
> > I'm trying to get a new dataframe or whatever to call, which has the same
> > structure with each file as listed above. For each cell in the new
> > dataframe or the new file, it is the average value from former dataframes
> > at the same location. Thanks.
> >
> >
> >
> > On Tue, May 9, 2017 at 8:41 AM, Doran, Harold < [hidden email]> wrote:
> >
> > Are you trying to take the mean over all cells, or over rows/columns
> > within each dataframe. Also, are these different dataframes stored
> within a
> > list or are they standalone?
> >
> >
> >
> >
> > -----Original Message-----
> > From: R-help [mailto: [hidden email]] On Behalf Of lily li
> > Sent: Tuesday, May 09, 2017 10:39 AM
> > To: R mailing list < [hidden email]>
>
> > Subject: [R] About calculating average values from several matrices
> >
> > Hi R users,
> >
> > I have a question about manipulating the data.
> > For example, there are several such data frames or matrices, and I want
> to
> > calculate the average value from all the data frames or matrices. How to
> do
> > it? Also, should I convert them to data frame or matrix first? Right now,
> > when I use typeof() function, each one is a list.
> >
> > file1
> > jan feb mar apr may jun jul aug sep oct
> nov
> >
> > app1 1.1 1.2 0.8 0.9 1.3 1.5 2.2 3.2 3.0 1.2
> 1.1
> > app2 3.1 3.2 2.8 2.5 2.3 2.5 3.2 3.0 2.9 1.8
> 1.8
> > app3 5.1 5.2 3.8 4.9 5.3 5.5 5.2 4.2 5.0 4.2
> 4.1
> >
> > file2
> > jan feb mar apr may jun jul aug sep oct
> nov
> >
> > app1 1.9 1.5 0.5 0.9 1.2 1.8 2.5 3.7 3.2 1.5
> 1.6
> > app2 3.5 3.7 2.3 2.2 2.5 2.0 3.6 3.2 2.8 1.2
> 1.4
> > app3 5.5 5.0 3.5 4.4 5.4 5.6 5.3 4.4 5.2 4.3
> 4.2
> >
> > file3 has the similar structure and values...
> >
> > There are eight such files, and when I use the function mean(file1,
> file2,
> > file3, ..., file8), it returns the error below. Thanks for your help.
> >
> > Warning message:
> > In mean.default(file1, file2, file3, file4, file5, file6, file7, :
> > argument is not numeric or logical: returning NA
> >
> > [[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-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.
|
|
Dear Lily,
Harold is telling you to type "?round" at the R command prompt to pull
up the "round" help page.
>?round
>help("round")
AFAIK, the above two commands are equivalent, in general.
Best, Bill.
W. Michels, Ph.D.
On Tue, May 9, 2017 at 8:11 AM, Doran, Harold < [hidden email]> wrote:
> ?round
>
>
> From: lily li [mailto: [hidden email]]
> Sent: Tuesday, May 09, 2017 11:10 AM
> To: Charles Determan < [hidden email]>
> Cc: Doran, Harold < [hidden email]>; R mailing list < [hidden email]>
> Subject: Re: [R] About calculating average values from several matrices
>
> Thanks very much, it works. But how to round the values to have only 1 decimal digit or 2 decimal digits? I think by dividing, the values are double type now. Thanks again.
>
>
> On Tue, May 9, 2017 at 9:04 AM, Charles Determan < [hidden email]<mailto: [hidden email]>> wrote:
> If you want the mean of each element across you list of matrices the following should provide what you are looking for where Reduce sums all your matrix elements across matrices and the simply divided my the number of matrices for the element-wise mean.
>
> Reduce(`+`, mylist)/length(mylist)
> Regards,
> Charles
>
> On Tue, May 9, 2017 at 9:52 AM, lily li < [hidden email]<mailto: [hidden email]>> wrote:
> I meant for each cell, it takes the average from other dataframes at the
> same cell. I don't know how to deal with row names and col names though, so
> it has the error message.
>
> On Tue, May 9, 2017 at 8:50 AM, Doran, Harold < [hidden email]<mailto: [hidden email]>> wrote:
>
>> It’s not clear to me what your actual structure is. Can you provide
>> str(object)? Assuming it is a list, and you want the mean over all cells or
>> columns, you might want like this:
>>
>>
>>
>> myData <- vector("list", 3)
>>
>>
>>
>> for(i in 1:3){
>>
>> myData[[i]] <- matrix(rnorm(100), 10, 10)
>>
>> }
>>
>>
>>
>> ### mean over all cells
>>
>> sapply(myData, function(x) mean(x))
>>
>>
>>
>> ### mean over all columns
>>
>> sapply(myData, function(x) colMeans(x))
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> *From:* lily li [mailto: [hidden email]<mailto: [hidden email]>]
>> *Sent:* Tuesday, May 09, 2017 10:44 AM
>> *To:* Doran, Harold < [hidden email]<mailto: [hidden email]>>
>> *Cc:* R mailing list < [hidden email]<mailto: [hidden email]>>
>> *Subject:* Re: [R] About calculating average values from several matrices
>
>>
>>
>>
>> I'm trying to get a new dataframe or whatever to call, which has the same
>> structure with each file as listed above. For each cell in the new
>> dataframe or the new file, it is the average value from former dataframes
>> at the same location. Thanks.
>>
>>
>>
>> On Tue, May 9, 2017 at 8:41 AM, Doran, Harold < [hidden email]<mailto: [hidden email]>> wrote:
>>
>> Are you trying to take the mean over all cells, or over rows/columns
>> within each dataframe. Also, are these different dataframes stored within a
>> list or are they standalone?
>>
>>
>>
>>
>> -----Original Message-----
>> From: R-help [mailto: [hidden email]<mailto: [hidden email]>] On Behalf Of lily li
>> Sent: Tuesday, May 09, 2017 10:39 AM
>> To: R mailing list < [hidden email]<mailto: [hidden email]>>
>> Subject: [R] About calculating average values from several matrices
>>
>> Hi R users,
>>
>> I have a question about manipulating the data.
>> For example, there are several such data frames or matrices, and I want to
>> calculate the average value from all the data frames or matrices. How to do
>> it? Also, should I convert them to data frame or matrix first? Right now,
>> when I use typeof() function, each one is a list.
>>
>> file1
>> jan feb mar apr may jun jul aug sep oct nov
>>
>> app1 1.1 1.2 0.8 0.9 1.3 1.5 2.2 3.2 3.0 1.2 1.1
>> app2 3.1 3.2 2.8 2.5 2.3 2.5 3.2 3.0 2.9 1.8 1.8
>> app3 5.1 5.2 3.8 4.9 5.3 5.5 5.2 4.2 5.0 4.2 4.1
>>
>> file2
>> jan feb mar apr may jun jul aug sep oct nov
>>
>> app1 1.9 1.5 0.5 0.9 1.2 1.8 2.5 3.7 3.2 1.5 1.6
>> app2 3.5 3.7 2.3 2.2 2.5 2.0 3.6 3.2 2.8 1.2 1.4
>> app3 5.5 5.0 3.5 4.4 5.4 5.6 5.3 4.4 5.2 4.3 4.2
>>
>> file3 has the similar structure and values...
>>
>> There are eight such files, and when I use the function mean(file1, file2,
>> file3, ..., file8), it returns the error below. Thanks for your help.
>>
>> Warning message:
>> In mean.default(file1, file2, file3, file4, file5, file6, file7, :
>> argument is not numeric or logical: returning NA
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email]<mailto: [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]<mailto: [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-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.
|
|