0

I have a matrix with two columns some of the numbers are the same in both columns, but column 2 also contains some numbers which are not in column 1.

I would like to select those values in column2 that are not in column1 and insert them in column1 in increasing order.

As a start I was thinking of using some matrix operation like matrix[matrix[,1]%in%matrix[,2]

just instead of %in% using something for "not in".

Here's the datafile:

https://dl.dropbox.com/u/22681355/example.csv

example<-read.csv("example.csv")

example[,2] contains some numbers which example[,1] does not.

I would like to:

  1. search for these numbers using something equivalent to %not in %

Following the answer below I can do the following:

values<-setdiff(example[,2],example[,1]

order<-sort(values)
user1723765
  • 6,179
  • 18
  • 57
  • 85

1 Answers1

2

Like this?

Rgames> foo
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    5    9   13   17
[2,]    2    6   10   14   18
[3,]    3    7    1   15   19
[4,]    4    8    3   16   20
Rgames> foo[,1]%in%foo[,3]
[1]  TRUE FALSE  TRUE FALSE
Rgames> foo[,1]*!foo[,1]%in%foo[,3]
[1] 0 2 0 4

I'm sure there's a cleaner way. Heck, just do this:

Rgames> setdiff(foo[,1],foo[,3])
[1] 2 4
Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73
  • great but then I would like to insert these items into foo[,3] such that it follows numerical order – user1723765 Dec 21 '12 at 13:15
  • @Joris Meys, why is it not clear? Two columns, 2 has some numbers that 1 doesn't. Find those numbers, place them in the first column. – user1723765 Dec 21 '12 at 13:16
  • @user1723765 Where in the first column? Add them at the end, insert them at the beginning, replace the numbers somewhere in the middle? Which values of the first column have to go? Or do you keep all from the first column? Or none, and you fill it up with NA, or... No, it's not clear what you want. – Joris Meys Dec 21 '12 at 13:22
  • first column originally has 1,3,5 and the missing values were 2 and 4 then the new first column should be 1,2,3,4,5. – user1723765 Dec 21 '12 at 13:31
  • 1
    that's clear. Now you only have to explain how the values of the other columns need to pair up, or whether you want to drop them. And what do you do with the fact that the original column has 4 values, and the new column 5, effectively adding a row to the complete matrix... – Joris Meys Dec 21 '12 at 13:37