[R] R loop

From: ivo welch <ivowel_at_gmail.com>
Date: Tue 07 Feb 2006 - 07:06:55 EST

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 );



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 07:14:20 2006

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