[R] for loop incorrect row count

Kevin Kowitski k.kowitski at icloud.com
Thu Jun 11 21:25:55 CEST 2015


Hey, 

  I am having an issue with a for loop that is intended to read index values by row and column so that it can pull out the valuable information.  My issue is that I am using a data.frame(which(df==1, arr.ind=TRUE)) to find the index of the values in my data frame that are equal to 1.  This outputs a data frame of 71 rows which is confirmed by the "nrows" function.  However, when I try to break up the rows and columns using the code below I am producing two vectors of 75 values, even though there are only 71 and the for loop is from 1 to the value of 71.  Am I making this task more complicated than it needs to be? 

if(countRaw > 0){
			index_R_df<-rbind(index_R_df,data.frame(which(sapply(data2[0:24,], match, INDString, nomatch=0)==1, arr.ind=TRUE)))
			index_lengthR<-nrow(index_R_df)
			
				for (j in 1:index_lengthR){
					index_rowsR<-c(index_rowsR, index_R_df[j,1])
					index_colsR<-c(index_colsR, index_R_df[j,2])
					#rowsPass_R<-c(unique(index_rowsR))
					#collect_rows<-c(collect_rows, rowsPass_R)
					}

I'm sorry if this seems very novice, I'm new to R. 

-Kevin


More information about the R-help mailing list