[R] Finding index of specific values in a data.frame

David Winsemius dwinsemius at comcast.net
Wed Jun 10 22:41:52 CEST 2015


On Jun 10, 2015, at 1:00 PM, Kevin Kowitski wrote:

> Oh I see, I'm sorry I just plopped it in GitHub for ease of help, I didn't notice I put it under coursera work. This task is not related to coursera, I will separate it out. 
> 
> -Kevin
> 
> Sent from my iPhone
> 
>> On Jun 10, 2015, at 3:21 PM, David Winsemius <dwinsemius at comcast.net> wrote:
>> 
>> 
>>> On Jun 10, 2015, at 9:41 AM, Kevin Kowitski wrote:
>>> 
>>> Hey everyone, 
>>> 
>>> I am new to R and I am trying to find the index of all of the values in a data.frame.   I have a .csv file that outputs pass, fail, error, and indeterminate readings.  I have passed the data from the .csv to a data.frame, have performed the proper matching criteria to generate a data.frame of 0's and 1's, and am outputting the total 1's (therefore matches) found.  I would also like to find the index of these values so that I can output a matrix containing the date and data point which has produced that match. Can anyone help set me in the right direction?

If you have a data.frame of all 1's and 0's, then this should give you the row and column indices of the 1's:

which(df==1, arr.ind=TRUE)

Just to test my presumption that the "==" function would coerce to a matrix suitable for the array index parameter to be effective, I tried with an available dataset: iris:

> str( which( iris[-5] > 3, arr.ind=TRUE) )
 int [1:316, 1:2] 1 2 3 4 5 6 7 8 9 10 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:2] "row" "col"

Further help will require presentation using dput of a minimal reproducible dataset to work on. Github is not a bad way to deliver this but presenting pages of code is not a good way to present a problem.

-- 
David.

>>> 
>>> here is a github link to the code I have already generated for more clarity on the project:
>>> 
>>> https://github.com/KevinKowitski/datasciencecoursera/blob/master/ErrorCount.R
>> 
>> I think the coursera homework assignments are supposed to be discussed in a course-provided web-mediated mailing list.
>> 
>> It's unclear from the presentation why the `which` and `%in%` do not provide a solution. 
>>> 
>>> Thank you, 
>>> 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.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list