Ok, so I'm new to R, but this is driving me crazy.  In this example, I
am trying to process each element in a list.

<code>
s = "1,2"
l = strsplit(s, ",", fixed=TRUE)
print("BEGIN")
n = length(l)
i = 1
while (i <= n) {
  x = l[[i]]
  print(paste("x:", class(x), x))
  print("BEFORE PRINT")
  print(x)
  print("AFTER PRINT")
  i = i + 1
}
</code>

<actual output>
     [exec] [1] "BEGIN"
     [exec] [1] "x: character 1" "x: character 2"
     [exec] [1] "BEFORE PRINT"
     [exec] [1] "1" "2"
     [exec] [1] "AFTER PRINT"
     [exec] [1] "END"
     [exec] [1] TRUE
</actual output>

<expected output>
     [exec] [1] "BEGIN"
     [exec] [1] "x: character 1"
     [exec] [1] "BEFORE PRINT"
     [exec] [1] "1"
     [exec] [1] "AFTER PRINT"
     [exec] [1] "x: character 2"
     [exec] [1] "BEFORE PRINT"
     [exec] [1] "2"
     [exec] [1] "AFTER PRINT"
     [exec] [1] "END"
     [exec] [1] TRUE
</expected output>

What *basic* concept am I missing here?  The same thing happens with
for (x in l) and lapply(l, function(x) print(x)). Please help.

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to