I am not an R expert, but I think the R idea of this task is to work with a data frame (where you make a and b are columns), and then to iterate over the rows. data <- read.table( textConnection( "a b 1 name1 2 name2 3 name3"), header=TRUE) ; # just learned this one for (i in 1:nrow(data)) print( data[i,] ); or just data[1:3,] apropos, R does not support native hashes (like perl), unfortunately, but it has something similar: data$a=="b" # gives F T F data[ data$a == "name2" ] # does not work and gives a weird result, yuck subset(data, data$a=="name2") # this does work. data frame sorting by column (using order) could also be simpler to understand for novices---IMHO, there should be a wrapper function that is in standard R like data.sorted = sort( data, by=data$b ); regards, /iaw
On 2/6/06 3:06 PM, "ivo welch" <ivowel at gmail.com> wrote:> I am not an R expert, but I think the R idea of this task is to work > with a data frame (where you make a and b are columns), and then to > iterate over the rows. > > data <- read.table( textConnection( > "a b > 1 name1 > 2 name2 > 3 name3"), header=TRUE) ; # just learned this one > > for (i in 1:nrow(data)) print( data[i,] ); > or just data[1:3,] > > > apropos, R does not support native hashes (like perl), unfortunately, > but it has something similar: > > data$a=="b" # gives F T F > data[ data$a == "name2" ] # does not work and gives a weird result, yuckdata[data$a=="name2",] Notice the difference from what you did.> data frame sorting by column (using order) could also be simpler to > understand for novices---IMHO, there should be a wrapper function that > is in standard R like > data.sorted = sort( data, by=data$b );Look at ?order. Sean
> > data[ data$a == "name2" ] # does not work and gives a weird result, yuck > > data[data$a=="name2",] >sorry about this. I believe a few versions back, one could not subset data frames, so I did not even check what I wrote. Works now.> Look at ?order.I know. This is why I suggested only that we need a wrapper that plugs an order() functionality into the sort() function (which fortunately points in its docs to order)---but novices would find the sort syntax easier. regards, /iaw
Hi On 6 Feb 2006 at 15:57, ivo welch wrote: Date sent: Mon, 6 Feb 2006 15:57:30 -0500 From: ivo welch <ivowel at gmail.com> To: Sean Davis <sdavis2 at mail.nih.gov> Copies to: piet.vanremortel at gmail.com, r-help <r-help at stat.math.ethz.ch> Subject: Re: [R] R loop> > > data[ data$a == "name2" ] # does not work and gives a weird > > > result, yuck > > > > data[data$a=="name2",] > > > > sorry about this. I believe a few versions back, one could not subset > data frames, so I did not even check what I wrote. Works now.It depends on what you consider few versions back. I started with R vesion 1.2.0 about 10 years ago and I believe that data frame subsetting was done in **very** similar manner as it is performed now. Cheers Petr> > > Look at ?order. > > I know. This is why I suggested only that we need a wrapper that > plugs an order() functionality into the sort() function (which > fortunately points in its docs to order)---but novices would find the > sort syntax easier. > > regards, > > /iaw > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.htmlPetr Pikal petr.pikal at precheza.cz
Petr Pikal wrote:> Hi > > On 6 Feb 2006 at 15:57, ivo welch wrote: > > Date sent: Mon, 6 Feb 2006 15:57:30 -0500 > From: ivo welch <ivowel at gmail.com> > To: Sean Davis <sdavis2 at mail.nih.gov> > Copies to: piet.vanremortel at gmail.com, r-help <r-help at stat.math.ethz.ch> > Subject: Re: [R] R loop > > >>>>data[ data$a == "name2" ] # does not work and gives a weird >>>>result, yuck >>> >>> data[data$a=="name2",] >>> >> >>sorry about this. I believe a few versions back, one could not subset >>data frames, so I did not even check what I wrote. Works now. > > > It depends on what you consider few versions back. I started with R > vesion 1.2.0 about 10 yearsI bet 200$ (or EUR) you have not used R 10 years ago. ;-) People certainly remember the 1.0.0 release at the remarkable day 29-FEB-2000. 1.2.0 was released in December 2000, about 5 years ago. I started with 0.62.x in 1998. The oldest version I found on CRAN is a pre-alpha R.sea.hqx for the Mac dated 07-Nov-1996. Uwe Ligges> ago and I believe that data frame > subsetting was done in **very** similar manner as it is performed > now. > > Cheers > Petr > > >>>Look at ?order. >> >>I know. This is why I suggested only that we need a wrapper that >>plugs an order() functionality into the sort() function (which >>fortunately points in its docs to order)---but novices would find the >>sort syntax easier. >> >>regards, >> >>/iaw >> >>______________________________________________ >>R-help at stat.math.ethz.ch mailing list >>https://stat.ethz.ch/mailman/listinfo/r-help >>PLEASE do read the posting guide! >>http://www.R-project.org/posting-guide.html > > > Petr Pikal > petr.pikal at precheza.cz > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html