Skip to content

2 factor split and lapply

6 messages · Bert Gunter, Onur Uncu, arun

#
I believe you missed
 ?tapply
 which does what you want I think (in the absence of a reproducible
example one cannot be sure).

Cheers,
Bert



Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
H. Gilbert Welch
On Sun, Dec 22, 2013 at 3:54 PM, Onur Uncu <onuruncu at gmail.com> wrote:
#
Sure, here is a reproducible example:

testframe<-data.frame(factor1=c("a","b","a"),factor2=c(1,2,2),data=c(3.34,4.2,2.1))

splitframe<-split(testframe,list(factor1=testframe$factor1,factor2=testframe$factor2))

lapply(splitframe,function(x)mean(x[,"data"]))

The above lapply returns

$a.1
[1] 3.34

$b.1
[1] NaN

$a.2
[1] 2.1

$b.2
[1] 4.2

The results are correct but not presented in a format I prefer... Factor1 and factor2 are combined into a single factor, which is not desired. I want to keep them seperate. Ideally, a table output as below.

     a          b
1   3.34     NaN
2   2.1       4.2

How can I achieve this please?
#
Hi,
You could try:
library(reshape2)
dcast(as.data.frame(as.table(by(testframe[,3],testframe[,-3],mean))),factor2~factor1,value.var="Freq")
#? factor2??? a?? b
#1?????? 1 3.34? NA
#2?????? 2 2.10 4.2

A.K.
On Monday, December 23, 2013 9:24 AM, Onur Uncu <onuruncu at gmail.com> wrote:
Sure, here is a reproducible example:

testframe<-data.frame(factor1=c("a","b","a"),factor2=c(1,2,2),data=c(3.34,4.2,2.1))

splitframe<-split(testframe,list(factor1=testframe$factor1,factor2=testframe$factor2))

lapply(splitframe,function(x)mean(x[,"data"]))

The above lapply returns

$a.1
[1] 3.34

$b.1
[1] NaN

$a.2
[1] 2.1

$b.2
[1] 4.2

The results are correct but not presented in a format I prefer... Factor1 and factor2 are combined into a single factor, which is not desired. I want to keep them seperate. Ideally, a table output as below.

? ?  a? ? ? ? ? b
1?  3.34? ?  NaN
2?  2.1? ? ?  4.2

How can I achieve this please?
______________________________________________
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.
#
HI,
I think this will be more appropriate.

dcast(testframe,factor2~factor1,value.var="data",mean)
? factor2??? a?? b
1?????? 1 3.34 NaN
2?????? 2 2.10 4.2
A.K.
On Monday, December 23, 2013 9:37 AM, arun <smartpink111 at yahoo.com> wrote:
Hi,
You could try:
library(reshape2)
dcast(as.data.frame(as.table(by(testframe[,3],testframe[,-3],mean))),factor2~factor1,value.var="Freq")
#? factor2??? a?? b
#1?????? 1 3.34? NA
#2?????? 2 2.10 4.2

A.K.
On Monday, December 23, 2013 9:24 AM, Onur Uncu <onuruncu at gmail.com> wrote:
Sure, here is a reproducible example:

testframe<-data.frame(factor1=c("a","b","a"),factor2=c(1,2,2),data=c(3.34,4.2,2.1))

splitframe<-split(testframe,list(factor1=testframe$factor1,factor2=testframe$factor2))

lapply(splitframe,function(x)mean(x[,"data"]))

The above lapply returns

$a.1
[1] 3.34

$b.1
[1] NaN

$a.2
[1] 2.1

$b.2
[1] 4.2

The results are correct but not presented in a format I prefer... Factor1 and factor2 are combined into a single factor, which is not desired. I want to keep them seperate. Ideally, a table output as below.

? ?? a? ? ? ? ? b
1?? 3.34? ?? NaN
2?? 2.1? ? ?? 4.2

How can I achieve this please?
______________________________________________
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.


______________________________________________
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.
#
As I said, ?tapply gives you an answer (without using other packages) . Read it.

-- Bert

Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
H. Gilbert Welch
On Mon, Dec 23, 2013 at 6:22 AM, Onur Uncu <onuruncu at gmail.com> wrote: