If you know the names will always work (i.e., names of y are always names of
x and the rows always have names) this should do it:
x[!(rownames(x) %in% rownames(y)),]
I.e., those rows of x such that their names are NOT included in the row
names of y.
Hope this helps,
Michael Weylandt
On Sat, Aug 6, 2011 at 8:20 AM, Henning Jensen <henning-j@gmx.de> wrote:
> Dear List,
>
>
>
> I am a beginner of R and have an easy question, which I couldn’t find out.
>
> I like to exclude all rows of matrix “y” from matrix ”x” (like a subset of
> “x”, without “y”).
>
>
>
> The matrix “x” is of the structure
>
> >str(x)
>
> num [1:346, 1:8] 0.055 0.6833 0.9121 0.0819 0.1223 ...
>
> - attr(*, "dimnames")=List of 2
>
> ..$ : chr [1:346] "10357" "10815" "10829"
"10885" ...
>
> ..$ : chr [1:8] "ua_mfb" "ua_mivh" "ua_mukh"
"ua_schwh" ...
>
>
>
> The matrix “y” is a subset of “x”
>
> > str(y)
>
> num [1:301, 1:8] 0.683 0.912 0.832 0.833 1 ...
>
> - attr(*, "dimnames")=List of 2
>
> ..$ : chr [1:301] "10815" "10829" "12049"
"13960" ...
>
> ..$ : chr [1:8] "ua_mfb" "ua_mivh" "ua_mukh"
"ua_schwh" ...
>
>
>
> Now I need a matrix “w” of all rows of “x”, which are not included in “y”.
>
> “w” seems to obtain the dimension nrow=45, ncol=8
>
>
>
>
>
> Example:
>
>
>
> I have a matrix ’x’:
>
>
>
> > x <- matrix(1:21,ncol=3)
>
> > rownames(x) <-
c("a","b","c","d","e","f","g");
colnames(x) <-
> c("V1","V2","V3")
>
> > x
>
> V1 V2 V3
>
> a 1 8 15
>
> b 2 9 16
>
> c 3 10 17
>
> d 4 11 18
>
> e 5 12 19
>
> f 6 13 20
>
> g 7 14 21
>
>
>
>
>
> And the subset ‘y’:
>
>
>
> y <- x[-c(1,3,5,7),]
>
> > y
>
> V1 V2 V3
>
> b 2 9 16
>
> d 4 11 18
>
> f 6 13 20
>
>
>
>
>
> Now I like to exclude all rows from x, which are included in y. The result
> has to be:
>
> > w
>
> V1 V2 V3
>
> a 1 8 15
>
> c 3 10 17
>
> e 5 12 19
>
> g 7 14 21
>
>
>
>
>
> I tried it with:
>
>
>
> l <- rownames(y)
>
> > w <- x[-c(get(l)),]
>
> Fehler in get(l) : Objekt 'b' nicht gefunden
>
>
>
> But R couldn’t find object “b”.
>
>
>
> So I tried it with a loop:
>
> > l <- rownames(y)
>
> > w <- NULL
>
> > for(i in l)
>
> + w[i] <- x[-(l),]
>
> Fehler in -(l) : ungültiges Argument für unären Operator
>
>
>
> But R returned: Error in –(l): argument invalid
>
>
>
> I also started experiments with get(paste()), merge, match() and subset(),
> but I couldn’t solve the problem jet.
>
> I would be very thankfull for a hint (->command) or a solution.
>
>
>
>
>
> Thanks in advance, Henning!
>
> Henning-j@gmx.de
>
>
>
>
>
>
> [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>
[[alternative HTML version deleted]]