Philipp Chapkovski
2012-Jan-08 17:03 UTC
[R] choosing items from array: very stupid and simple question
x<-1:11 y<-2:3 is there a way to do something like x[x==y] (which actually produce error)? I am really got stuck
Sarah Goslee
2012-Jan-08 17:09 UTC
[R] choosing items from array: very stupid and simple question
> x[x %in% y][1] 2 3>2012/1/8 Philipp Chapkovski <chapkovski at gmail.com>:> x<-1:11 > y<-2:3 > > is there a way to do something like > x[x==y] > (which actually produce error)? > I am really got stuck >-- Sarah Goslee http://www.functionaldiversity.org
Richard M. Heiberger
2012-Jan-08 17:19 UTC
[R] choosing items from array: very stupid and simple question
This does literally what you asked for> x[x %in% y][1] 2 3 This is more likely what you want> which(x %in% y)[1] 2 3>It is an artifact of this example that the two results look the same. Here is a different example that distinguishes them, and in this example, %in% is probably not right either. match() would be better.> x <- sample(100, 11) > x[1] 99 76 97 65 20 70 32 6 80 16 51> y <- c(65, 99) > y[1] 65 99> x %in% y[1] TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE> x[x %in% y][1] 99 65> which(x %in% y)[1] 1 4> mm <- match(y, x, 0) > mm[1] 4 1> x[mm][1] 65 99>We care because this is only useful if there is more than one column.> xx <- cbind(x, 11:21) > xx[mm,]x [1,] 65 14 [2,] 99 11> > ## compare to > xx[which(x %in% y),] ## same rows but in wrong orderx [1,] 99 11 [2,] 65 14>Rich 2012/1/8 Philipp Chapkovski <chapkovski@gmail.com>> x<-1:11 > y<-2:3 > > is there a way to do something like > x[x==y] > (which actually produce error)? > I am really got stuck > > ______________________________________________ > 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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Possibly Parallel Threads
- how to combine two data frames via factors (or somehow else)
- how to just download file on disk by the link
- length of 'dimnames' [2] not equal to array extent- For Correlation Plot
- length of 'dimnames' [2] not equal to array extent- For Correlation Plot
- Can I define a object array in R?