[R] R loop

ivo welch ivowel at gmail.com
Mon Feb 6 21:06:55 CET 2006


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




More information about the R-help mailing list