Ff Yy
Ff Yy

Reputation: 247

Subsetting a vector with a logical condition

> x1=c(4,5,6,7,8)
> x1
[1] 4 5 6 7 8
> x2=x1[x1!=6]
> x2
[1] 4 5 7 8
> x3=x1[x1=6]
> x3
[1] NA

Why is x3 not 6? I don't understand.

Upvotes: 4

Views: 20449

Answers (1)

Maiasaura
Maiasaura

Reputation: 32986

<- and = are assignment operators. By using x1[x1=6], you are assigning the value of 6 to x1, not checking whether they match. Type in ?assignOps at the R prompt for more info. Use == instead.

x3 <- x1[x1==6]

Upvotes: 8

Related Questions