1

I am getting an 'integer(0)' result for the following query in one instance of my code, but it works fine otherwise:

data.dat:

xx, linear, squared, gaussian, rando, fruit, color, type, xxx, yyy

1, 1, 1, 1, 1.1, apple, blue, gold, 1, 1

2, 3, 4, 1, 2.5, apple, red, gold, 2, 1

3, 2, 9, 2, 4.4, orange, blue, silver, 1, 1

4, 4, 16, 3, 5.9, orange, blue, gold, 1, 1

5, 5, 25, 5, 5.5, peach, blue, gold, 1, 1

6, 6, 32, 12, 6.9, peach, blue, gold, 1, 2

7, 7, 48, 24, 7.2, apple, blue, silver, 1, 1

8, 9, 66, 30, 7.4, apple, blue, gold, 1, 2

9, 8, 84, 31, 7.6, pear, red, gold, 1, 1

10,10, 102, 30, 1.5, orange, red, gold, 1, 1

data2 <- read.csv(file="data.dat",head=TRUE,sep=",");
which(data2$color=="red" , arr.ind=TRUE)

This isn't working either:

which(as.character(data2$color)=="red" , arr.ind=TRUE)

I feel like I'm losing my mind, I've used this function hundreds of times with no issue...

10 Rep
  • 2,217
  • 7
  • 19
  • 33
Entropy
  • 133
  • 2
  • 12
  • 3
    Looks like you have colors " red" and " blue" rather than "red" and "blue" – Luke C Dec 04 '17 at 23:50
  • 2
    Could have used `grep("red", data2$color)`. BTW, Using `arr.ind=TRUE` with a vector argument makes no sense. – IRTFM Dec 04 '17 at 23:59

1 Answers1

6

You have an extra whitespace in your entries. Try:

which(trimws(data2$color) == "red")
Maurits Evers
  • 49,617
  • 4
  • 47
  • 68