[R] 2 factor split and lapply
Onur Uncu
onuruncu at gmail.com
Mon Dec 23 15:22:36 CET 2013
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?
> On 23 Dec 2013, at 00:44, Bert Gunter <gunter.berton at gene.com> wrote:
>
> 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:
>> R Users,
>>
>> I have a data frame which I split using 2 factors using the split function:
>>
>> split(datframe, list(f=factor1, f2=factor2));
>>
>> I then used lapply to get some summary statistics grouped by factor1 and
>> factor2.
>>
>> I now want to change the appearance of this output. I want to get a 2
>> dimensional table where columns represent values of factor1, rows represent
>> values of factor2 and the entries on the table represent the summary
>> results that were calculated by lapply.
>>
>> I tried as.table() function but did not help. It seems the problem is that
>> R combined factor1 and factor 2 into one factor when I used list(f=factor1,
>> f2=factor2) in the split function. So R is now unable to treat them as 2
>> different factors in order to put them on row and columns of a table... Any
>> ideas how I can achieve the desired table?
>>
>> Thanks for your help.
>>
>> [[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.
More information about the R-help
mailing list