[R] aggregate output to data frame

Jim Lemon drj|m|emon @end|ng |rom gm@||@com
Thu Mar 28 03:39:38 CET 2019


Hi Cyrus,
Try this:

pcr<-data.frame(Ct=runif(66,10,20),Gene=rep(LETTERS[1:22],3),
 Type=rep(c("Std","Unkn"),33),Rep=rep(1:3,each=22))
testagg<-aggregate(pcr$Ct,c(pcr["Gene"],pcr["Type"],pcr["Rep"]),
 FUN=function(x){c(mean(x), sd(x), sd(x)/sqrt(sd(x)))})
nxcol<-dim(testagg$x)[2]
newxs<-paste("x",1:nxcol,sep="")
for(col in 1:nxcol)
 testagg[[newxs[col]]]<-testagg$x[,col]
testagg$x<-NULL

Jim

On Thu, Mar 28, 2019 at 12:39 PM cir p via R-help <r-help using r-project.org> wrote:
>
> Dear users,
> i am trying to summarize data using "aggregate" with the following command:
>
> aggregate(pcr$Ct,c(pcr["Gene"],pcr["Type"],pcr["Rep"]),FUN=function(x){c(mean(x), sd(x), sd(x)/sqrt(sd(x)))})
>
> and the structure of the resulting data frame is
>
> 'data.frame':    66 obs. of  4 variables:
> $ Gene: Factor w/ 22 levels "14-3-3e","Act5C",..: 1 2 3 4 5 6 7 8 9 10 ...
> $ Type: Factor w/ 2 levels "Std","Unkn": 2 2 2 2 2 2 2 2 2 2 ...
> $ Rep : int  1 1 1 1 1 1 1 1 1 1 ...
>  $ x   : num [1:66, 1:3] 16.3 16.7 18.2 17.1 18.6 ...
>
> The actual data is "bundled" in a matrix $x of the data frame. I would like to have the columns of this matrix as individual numeric columns in the same data frame instead of a matrix, but cant really figure it out how to do this in an efficient way. Could someone help me with the construction of this?
>
> Thanks a lot,
>
> Cyrus
>
> ______________________________________________
> 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.



More information about the R-help mailing list