when do I need to use which()?> a <- c(1,2,3,4,5,6) > a[1] 1 2 3 4 5 6> a[a==4][1] 4> a[which(a==4)][1] 4> which(a==4)[1] 4> a[which(a>2)][1] 3 4 5 6> a[a>2][1] 3 4 5 6>seems unnecessary... -- Sam Steingold (http://sds.podval.org/) on CentOS release 5.6 (Final) X 11.0.60900031 http://jihadwatch.org http://palestinefacts.org http://mideasttruth.com http://truepeace.org http://thereligionofpeace.com Good programmers treat Microsoft products as damage and route around it.
Well ... which(a==4)^2 ?? -- Bert On Tue, Jul 12, 2011 at 1:17 PM, Sam Steingold <sds at gnu.org> wrote:> when do I need to use which()? >> a <- c(1,2,3,4,5,6) >> a > [1] 1 2 3 4 5 6 >> a[a==4] > [1] 4 >> a[which(a==4)] > [1] 4 >> which(a==4) > [1] 4 >> a[which(a>2)] > [1] 3 4 5 6 >> a[a>2] > [1] 3 4 5 6 >> > > seems unnecessary... > > -- > Sam Steingold (http://sds.podval.org/) on CentOS release 5.6 (Final) X 11.0.60900031 > http://jihadwatch.org http://palestinefacts.org http://mideasttruth.com > http://truepeace.org http://thereligionofpeace.com > Good programmers treat Microsoft products as damage and route around it. > > ______________________________________________ > R-help at 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. >-- "Men by nature long to get on to the ultimate truths, and will often be impatient with elementary studies or fight shy of them. If it were possible to reach the ultimate truths without the elementary studies usually prefixed to them, these would not be preparatory studies but superfluous diversions." -- Maimonides (1135-1204) Bert Gunter Genentech Nonclinical Biostatistics
On Jul 12, 2011, at 4:17 PM, Sam Steingold wrote:> when do I need to use which()? >> a <- c(1,2,3,4,5,6) >> a > [1] 1 2 3 4 5 6 >> a[a==4] > [1] 4 >> a[which(a==4)] > [1] 4 >> which(a==4) > [1] 4 >> a[which(a>2)] > [1] 3 4 5 6 >> a[a>2] > [1] 3 4 5 6 >> > > seems unnecessary... >It is unnecessary when `a` is a toy case and has no NA's. And you will find some of the cognoscenti trying to correct you when you do use which(). a <- c(1,2, NA ,3,4, NaN, 5,6) > data.frame(lets= letters[1:8], stringsAsFactors=FALSE)[a>0, ] [1] "a" "b" NA "d" "e" NA "g" "h" > data.frame(lets= letters[1:8], stringsAsFactors=FALSE)[which(a>0), ] [1] "a" "b" "d" "e" "g" "h" If you have millions of records and tens of thousands of NA's (say ~ 1% of the data), imagine what your console looks like if you try to pick out records from one day and get 10,000 where you were expecting 100. A real PITA when you are doing real work. -- David Winsemius, MD West Hartford, CT
On Tue, Jul 12, 2011 at 1:17 PM, Sam Steingold <sds at gnu.org> wrote:> when do I need to use which()?See ?which For examples, try: example(which)>> a <- c(1,2,3,4,5,6) >> a > [1] 1 2 3 4 5 6 >> a[a==4] > [1] 4 >> a[which(a==4)] > [1] 4 >> which(a==4) > [1] 4 >> a[which(a>2)] > [1] 3 4 5 6 >> a[a>2] > [1] 3 4 5 6 >> > > seems unnecessary...Yes, it can be used as a redudant wrapper as you have demonstrated in your examples. In those cases, it is most certainly unnecessary.> > -- > Sam Steingold (http://sds.podval.org/) on CentOS release 5.6 (Final) X 11.0.60900031 > http://jihadwatch.org http://palestinefacts.org http://mideasttruth.com > http://truepeace.org http://thereligionofpeace.com > Good programmers treat Microsoft products as damage and route around it. > > ______________________________________________ > R-help at 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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles https://joshuawiley.com/