[R] store the results of two connected and "disturbed" for-loops to data.frame

Rui Barradas ruipbarradas at sapo.pt
Wed Aug 15 17:50:35 CEST 2012


Hello,

Just start the loop in 'k' after the value for 'i'. Try the following.

nr <- ncol(var)
store<-data.frame(matrix(nrow=nr*(nr - 1)/2, ncol=2))
colnames(store)=c("usedVar","prediction")

a <- 0
for (i in c(1:4))
{
     for (k in (i:4)[-1])
     {
     dis<-lda(g~var[,i]+var[,k],CV=TRUE,fold=6)#linear discriminant function
     ct<-table(g,dis$class)
     pre<-sum(diag(prop.table(ct)))# Prediction of cv-reclassification

     #store the results
     a <- a + 1
     store[a,1]<-paste(label[i],label[k])
     store[a,2]<-round(pre,4)
     }
}
store

Also, 'var' is a bad name for a variable, it already is an R function name.

Hope this helps,

Rui Barradas

Em 15-08-2012 16:24, Thomas Schu escreveu:
> Dear all,
>
> here is a example of my problem:
>
> /#data#
> g<-c(1,1,1,2,2,2)
> A<-runif(6,min=1,max=5)
> B<-runif(6,min=100,max=1000)
> C<-runif(6,min=30,max=31)
> D<-runif(6,min=67,max=98765)
> var<-cbind(A,B,C,D)
> label<-colnames(var)
>
> store<-data.frame(matrix(ncol=2))
> colnames(store)=c("usedVar","prediction")
>
> library(MASS)#get lda
>
> for (i in c(1:4))
> {
> 	for (k in c(1:4))
> 	{
> 	if(i==k)break()#using the same variable will not work in lda
>
> 	dis<-lda(g~var[,i]+var[,k],CV=TRUE,fold=6)#linear discriminant function
> 	ct<-table(g,dis$class)
> 	pre<-sum(diag(prop.table(ct)))# Prediction of cv-reclassification
>
> 	#store the results
> 	*a<-i+4*(k-1)*
> 	store[a,1]<-paste(label[i],label[k])
> 	store[a,2]<-round(pre,4)
> 	}
> }
> store/
>
> I have got two for-loops getting "disturbed" by an if-break-case (disturbed
> means, that I don´t need the information for these cases).
> By using /*a<-i+4*(k-1)*/ for storing results to my "result"data.frame,  the
> break-cases will be included as NA too.
> *Are there a way to store my loop-results without the NA-cases to get an
> continous dataset??? *
>
> I´m pleased about every suggestion.
>
> Thank you and best regards
> Thomas
>
>
>
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/store-the-results-of-two-connected-and-disturbed-for-loops-to-data-frame-tp4640374.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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