Let's say I have a 4x2
matrix.
x<- matrix(seq(1:8), 4)
That contains the following elements
1 5
2 6
3 7
4 8
For this specific example, let's say I want to remove the rows that contain a '2' or an '7' (without having to manually look in the matrix and remove them). How would I do this?
Here's something I came up with but it isn't doing what I want it to. I want it to return the row indices in the matrix that contain either a 2
or a 7
.
remove<- which(2 || 7 %in% x)
x<- x[-remove,]
Can anyone help me figure this out?
which()
is the correct function, but you need to read a bit more about the indexing syntax. – Joanx[-which(...), ]
is dangerous in the event thatwhich
returns no match. Preferx[!(...), ]
. – Fatso