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
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
Oh, Swami, gazing into the crystal ball one can see ... ;-} Cheers, Bert Bert Gunter "Data is not information. Information is not knowledge. And knowledge is certainly not wisdom." -- Clifford Stoll On Thu, Jun 11, 2015 at 4:48 PM, David Winsemius <dwinsemius at comcast.net> wrote:> > 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 > > ______________________________________________ > 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. >[[alternative HTML version deleted]]
Hi Kevin, I don't even pretend to try to read it. If David is having a problem I am going for tea. I think we need some sample data (see ?dput for the best way to supply it) and an succinct description in English of what the problem is and what you need to get out of the data. I suspect because you are so used to programming in other languages you are jumping into one approach before you should. R can be very weird indeed for those used to working with "sensible" languages. Also have a look at http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example and http://adv-r.had.co.nz/Reproducibility.html for some general suggestions about questions on the list etc. BTW, the reason for using dput() is that it provides a 'perfect' copy of your data. Other ways of submitting sample data are less satisfactory. For example I tend to use 1 of 2 ways to read in text data especially if I am just grabbing some data from a problem on R-help and I may read in the data okay if I use one method or, if for some reason I use the other method I might read in some data as numeric that should be a factor, etc. which makes troubleshooting just a bit difficult. And which method I use can be a crap shoot. John Kane Kingston ON Canada> -----Original Message----- > From: dwinsemius at comcast.net > Sent: Thu, 11 Jun 2015 16:48:40 -0700 > To: k.kowitski at icloud.com > Subject: Re: [R] for loop incorrect row count > > > 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 > > ______________________________________________ > 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.____________________________________________________________ FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your desktop!