Entropy
Entropy

Reputation: 133

The which() function in R is not working for some reason

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

Upvotes: 1

Views: 2426

Answers (1)

Maurits Evers
Maurits Evers

Reputation: 50718

You have an extra whitespace in your entries. Try:

which(trimws(data2$color) == "red")

Upvotes: 6

Related Questions