[R] for loop incorrect row count

David Winsemius dwinsemius at comcast.net
Fri Jun 12 01:48:40 CEST 2015


On Jun 11, 2015, at 12:25 PM, Kevin Kowitski wrote:

> 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))

That would be coercing a matrix to a dataframe. But why?


> 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){

How the value of countRow enters into this is entirely unclear.

> 			index_R_df<-rbind( index_R_df, data.frame(which(sapply(data2[0:24,],

R does NOT use zero-based indexing.


> match, INDString, nomatch=0)==1, arr.ind=TRUE)))

You need to explain what you are doing here. It's a bit too obscure to me how we should know that index_R_df will line up with the items would drop out of:

data.frame(which(sapply(data2[0:24,],match, INDString, nomatch=0)==1, arr.ind=TRUE)))

I would have expected some `name` to be followed by `[` then `which(...)`


> 			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)
> 					}
> 

There are too many missing here for me to do anything useful. You are either only giving us a fragment of code and using zero based  indexing. Without a data example, I'm throwing it back to you ot someone in the readership with better intuition or imagination than I possess.


> I'm sorry if this seems very novice, I'm new to R. 
> 
> -Kevin
> ______________________________________________
> R-help at 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.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list