Mitra Rahmati
Mitra Rahmati

Reputation: 301

select some values of a row in a matrix

In R with a matrix:

m <- matrix(1:20, ncol = 4) 
colnames(m) <- letters[1:4]

In the case I want to select just one value, it works. Like this,

subset(m, m[,4] == 17)  

But if I want for example 3 values, I get an error. look at this:

subset(m, m[,4] == c(17,19,20,"|"))

any suggestion?

Upvotes: 2

Views: 236

Answers (2)

user1317221_G
user1317221_G

Reputation: 15441

without using subset

m[ m[,4] %in% c(17, 19, 20),]

Upvotes: 2

Jilber Urbina
Jilber Urbina

Reputation: 61154

Change == by %in%

subset(m, m[,4] %in% c(17,19,20))

Upvotes: 2

Related Questions