Re: [R] R loop

From: Sean Davis <sdavis2_at_mail.nih.gov>
Date: Tue 07 Feb 2006 - 07:51:47 EST

On 2/6/06 3:06 PM, "ivo welch" <ivowel@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, yuck

 data[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



R-help@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 Received on Tue Feb 07 08:03:59 2006

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:42:24 EST