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.
What is going on?
4 messages · Paul Johnston, Peter Alspach, Ben Bolker +1 more
Kia ora Paul length(l) is 1 - that is, it is a list of with one element. That list element is a character vector of length 2. HTH .... Peter Alspach
-----Original Message-----
From: r-help-bounces at r-project.org
[mailto:r-help-bounces at r-project.org] On Behalf Of Paul Johnston
Sent: Thursday, 12 February 2009 8:18 a.m.
To: r-help at r-project.org
Subject: [R] What is going on?
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 at 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.
The contents of this e-mail are confidential and may be subject to legal privilege. If you are not the intended recipient you must not use, disseminate, distribute or reproduce all or any part of this e-mail or attachments. If you have received this e-mail in error, please notify the sender and delete all material pertaining to this e-mail. Any opinion or views expressed in this e-mail are those of the individual sender and may not represent those of The New Zealand Institute for Plant and Food Research Limited.
The problem is that strsplit is designed to work on a *vector* of characters (your example is a length-1 vector of characters), each of which might end up being split into a character vector of different lengths, so it returns its results as a *list* the same length as the original character vector: in this case a list of length 1.
str(l)
List of 1 $ : chr [1:2] "1" "2" length(l) is 1; length(l[[1]]) is 2. Ben Bolker
Paul Johnston-6 wrote:
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 at 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.
View this message in context: http://www.nabble.com/What-is-going-on--tp21962284p21962681.html Sent from the R help mailing list archive at Nabble.com.
Lists. You're missing the "list" concept. I'm sure others can explain it better, but here's the basic idea. Take a look at l after you do the split:
l
[[1]] [1] "1" "2"
length(l)
[1] 1
strsplit() returns a list of length 1, with two elements, the two
split "bits" of your initial string. The help for strsplit() even says:
A list of length 'length(x)' the 'i'-th element of which contains
the vector of splits of 'x[i]'.
You can get the individual components that you expected with
l[[1]][1]
and
l[[1]][2]
This is confusing in the single case, but allows strsplit to work on multiple
strings:
s <- c("1,2", "3,4")
l = strsplit(s, ",", fixed=TRUE)
l
[[1]] [1] "1" "2" [[2]] [1] "3" "4"
l[[1]][1]
[1] "1"
l[[2]][1]
[1] "3"
length(l)
[1] 2
On Wed, Feb 11, 2009 at 2:18 PM, Paul Johnston <pcj127 at gmail.com> wrote:
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.
Sarah Goslee http://www.functionaldiversity.org