[R] R loop
Sean Davis
sdavis2 at mail.nih.gov
Mon Feb 6 21:51:47 CET 2006
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, 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
More information about the R-help
mailing list