0

I am trying to find rows in su19$q2 marked "Yes", and from there find its corresponding value (names of schools) in su19$q1. However, everything I am trying seems to not work.

su19$q1[su19$q2 == "Yes"]

I do not understand why this is not working.

My expectation is that for every "Yes" response in q2, a list is returned of its corresponding school from column q1.

OTStats
  • 1,820
  • 1
  • 13
  • 22

2 Answers2

1

This should work:

su19$q1[which(su19$q2 == "Yes")]

The which() function returns the indices where the given vector is "True". In your example the expression su19$q2 == "Yes" returns a vector of either True of False depending if the equality holds or not (see https://www.rdocumentation.org/packages/base/versions/3.6.1/topics/which)

Chelmy88
  • 1,106
  • 1
  • 6
  • 17
  • same result, but instead of working with vectors, I would return the entire row and select only the column I need `su19[which(su19$q2 == "Yes"),]$q1`. I found it more versatile as I have more control on the output. – fra Aug 08 '19 at 18:29
  • Thank you, but when I put in either of these it returns NULL. – Maria Smith Aug 08 '19 at 20:08
  • Can you then please show us what you have in su19 ? – Chelmy88 Aug 08 '19 at 21:18
  • I am not sure how to format the table in Stack overflow but in: q1, there is a list of school names (e.g. Hancock College, Marin College, Portland College); in q2, it is either yes or no. – Maria Smith Aug 08 '19 at 22:37
  • You can provide the output of `head(su19)`(which displays the first lines) and of `typeof(su19)` (which tells the object type) – Chelmy88 Aug 09 '19 at 05:40
  • 1
    check if `class(su19$q2)` returns `character`. If not try to convert it to a character vector `su19$q2 <- as.character(su19$q2)`, and re-run @Chelmy88 command – fra Aug 09 '19 at 09:28
  • @fra Yes, the class for q2 is character. – Maria Smith Aug 09 '19 at 10:13
  • Name of School 1 Santiago Canyon College 2 College of Alameda 3 Cerritos College 4 Cuyamaca college 5 Cypress College 6 Folsom Lake College Is this a Corrections to College School? 1 Yes 2 Yes 3 Yes 4 Yes 5 Yes 6 Yes – Maria Smith Aug 09 '19 at 10:17
  • I put Name of school into a variable q1; and Is this a Corrections to College School? into q2 – Maria Smith Aug 09 '19 at 10:18
  • It would be useful if you can really provide us with q1 and q2, e.g with `dput()` (see [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)) – Chelmy88 Aug 13 '19 at 08:35
0

I am creating a new answer as adding code is not possible in comments

su19 <- data.frame(q1 = c("Santiago Canyon College","College of 
       Alameda","Cerritos College","Cuyamaca college","Cypress College","Folsom Lake 
       College"), q2= c("Yes","Yes","Yes","Yes","Yes","Yes"),stringAsFactors= FALSE)

                        q1  q2
 1 Santiago Canyon College Yes
 2      College of Alameda Yes
 3        Cerritos College Yes
 4        Cuyamaca college Yes
 5         Cypress College Yes
 6     Folsom Lake College Yes

@Chelmy88 solution

su19$q1[which(su19$q2 == "Yes")]
[1] "Santiago Canyon College" "College of Alameda"      "Cerritos College"        "Cuyamaca college"        "Cypress College"        
[6] "Folsom Lake College" 

my solution

su19[which(su19$q2 == "Yes"),]$q1
[1] "Santiago Canyon College" "College of Alameda"      "Cerritos College"        "Cuyamaca college"        "Cypress College"        
[6] "Folsom Lake College" 

If you don't have a data.frame, but vectors only

q1 <- c("Santiago Canyon College","College of Alameda","Cerritos College","Cuyamaca college","Cypress College","Folsom Lake College")
q2 <- c("Yes","Yes","Yes","Yes","Yes","Yes")

su19$q1[which(su19$q2 == "Yes")]
[1] "Santiago Canyon College" "College of Alameda"      "Cerritos College"        "Cuyamaca college"        "Cypress College"        
[6] "Folsom Lake College" 
fra
  • 832
  • 6
  • 14
  • Thank you! It seems when I put the labels into variable, I receive a NULL output but when I level the col name in its original state, I receive the output I am looking for. thanks so much! – Maria Smith Aug 09 '19 at 21:34