[R] Please help me to short my code

arun smartpink111 at yahoo.com
Sat Nov 9 07:05:56 CET 2013


Hi,
Try either:
Ceramic <- read.table("ceramic.dat",header=TRUE)
Ceramic1 <- Ceramic
Ceramic$indx <- ((seq_len(nrow(Ceramic))-1)%/%60)+1
library(plyr)
DF1 <- data.frame(M=as.vector(t(ddply(Ceramic,.(indx), colwise(mean))[,-1])), S=as.vector(t(ddply(Ceramic,.(indx),colwise(sd))[,-1])),Rep = 60)
 colnames(DF)[3] <- colnames(DF1)[3]
 identical(DF,DF1)
#[1] TRUE


#or
 indx <- ((seq_len(nrow(Ceramic))-1)%/%60)+1
Ceramic2 <-  do.call(data.frame, c(aggregate(.~indx,data=Ceramic1,function(x) c(mean(x),sd(x))), check.names=FALSE))[,-1]
 DF2 <- data.frame(M= as.vector(t(Ceramic2[,seq(1,ncol(Ceramic2),by=2)])), S= as.vector(t(Ceramic2[,seq(2,ncol(Ceramic2),by=2)])),Rep =60)
identical(DF,DF2)
#[1] TRUE



A.K.


please help me to short the code 

#To import data onto R 
Ceramic<-read.table("D:/ceramic.dat",header=T) 
#to obtain mean, standard deviation and number of observations- 
LAB1<-Ceramic[1:60,] 
LAB2<-Ceramic[61:120,] 
LAB3<-Ceramic[121:180,] 
LAB4<-Ceramic[181:240,] 
LAB5<-Ceramic[241:300,] 
LAB6<-Ceramic[301:360,] 
LAB7<-Ceramic[361:420,] 
LAB8<-Ceramic[421:480,] 
M1<-sapply(LAB1,mean) 
M2<-sapply(LAB2,mean) 
M3<-sapply(LAB3,mean) 
M4<-sapply(LAB4,mean) 
M5<-sapply(LAB5,mean) 
M6<-sapply(LAB6,mean) 
M7<-sapply(LAB7,mean) 
M8<-sapply(LAB8,mean) 
S1<-sapply(LAB1,sd) 
S2<-sapply(LAB2,sd) 
S3<-sapply(LAB3,sd) 
S4<-sapply(LAB4,sd) 
S5<-sapply(LAB5,sd) 
S6<-sapply(LAB6,sd) 
S7<-sapply(LAB7,sd) 
S8<-sapply(LAB8,sd) 
#tabulating results- 
M<-c(M1,M2,M3,M4,M5,M6,M7,M8) 
S<-c(S1,S2,S3,S4,S5,S6,S7,S8) 
DF<-data.frame(M,S,c(rep(60)))



More information about the R-help mailing list