Double for loop in R -


i want create data set using following code, aim add blank row between each group, , 3 blank rows between each treatment. wish looks following dataset.

value group treatment  39.7             1  53.5             1  51.1             1  67.8             1   84.8     b         1  80.3     b         1  79.6     b         1  84.3     b         1     31.0     c         2  32.0     c         2  33.0     c         2  34.0     c         2    1.0     d         2   2.0     d         2   3.0     d         2   4.0     d         2 

i wrote down double for loop doesn't work well. can't figure out. can tell me problem it?

mydata <- data.frame(value=c(39.7,53.5,51.1,67.8,84.8,80.3,79.6,84.3,31,32,33,34,1,2,3,4),group=c("a","a","a","a","b","b","b","b","c","c","c","c","d","d","d","d"),treatment=c(1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2))  blank <- data.frame(value=na,group="na",treatment=na) blank1 <- data.frame(value=c(na,na,na),group=c("na","na","na"),treatment=c(na,na,na))  uni<-unique(mydata$treatment) b<-length(uni) out <- c() out1<-c()  (i in 1:b) {   emp<-subset(mydata,treatment==uni[i])   uni1<-unique(emp$group)   c<-length(uni1)   (j in 1:c){     emp1<-subset(subset(mydata,treatment==uni[i]),group==uni1[j])     stack<-rbind(emp1,blank)     out<-rbind(stack,out)   }   stack1<-rbind(out,blank1)   out1<-rbind(stack1,out1) } 

i can't imagine practical reason having data blank rows in such pattern, i'm assuming printing:

printables <- lapply(   split(mydata,mydata$treatment),   function(t)     split(t,as.character(t$group)))  (t in seq_along(printables)){   for(g in seq_along(printables[[t]])){     print(printables[[t]][[g]],row.names=false)     cat('\n')   }   cat('\n\n') } 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -