Dear all, I have a string, let's say "testing", and I would like to extract in sequence each letter (character) from it. But when I use substr() I only properly get the first character, the rest is empty (""). What am I getting wrong? For example, I have this code:>>>x <- "testing" k <- nchar(x) for (i in 1:k) { y <- substr(x, i, 1) print(y) } [[alternative HTML version deleted]]
On 21/01/18 21:59, Luigi Marongiu wrote:> Dear all, > I have a string, let's say "testing", and I would like to extract in > sequence each letter (character) from it. But when I use substr() I only > properly get the first character, the rest is empty (""). What am I getting > wrong?What you're getting wrong is failing the read the help for substr(). The third argument is "stop", not the length of the substring.> For example, I have this code: > >>>> > x <- "testing" > k <- nchar(x) > for (i in 1:k) { > y <- substr(x, i, 1) > print(y) > }You want y <- substr(x,i,i). cheers, Rolf Turner -- Technical Editor ANZJS Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276
On Sun, 2018-01-21 at 09:59 +0100, Luigi Marongiu wrote:> Dear all, > I have a string, let's say "testing", and I would like to extract in > sequence each letter (character) from it. But when I use substr() I only > properly get the first character, the rest is empty (""). What am I getting > wrong? > For example, I have this code: > > >>> > x <- "testing" > k <- nchar(x) > for (i in 1:k) { > y <- substr(x, i, 1) > print(y) > }>From the help pagesubstr(x, start, stop) where 'start' is the position in the character vector x at which the substring starts, and 'stop' is the position at which it stops. Hence 'stop' must be >= 'start'; and if they are equal then you get just the single character. That is the case in your code, when i=1; when i > 1 then stop < start, so you get nothing. Compare with: x <- "testing" k <- nchar(x) for (i in 1:k) { y <- substr(x, i, i) ### was: substr(x, i, 1) print(y) } [1] "t" [1] "e" [1] "s" [1] "t" [1] "i" [1] "n" [1] "g" Hoping this helps, Ted.
The reason you get "" is, as stated on the previous response and on the documentation of substr function, the function "When extracting, if start is larger than the string length then "" is returned.". This is what happens on your function. HTH EK On Sun, Jan 21, 2018 at 3:59 AM, Luigi Marongiu <marongiu.luigi at gmail.com> wrote:> Dear all, > I have a string, let's say "testing", and I would like to extract in > sequence each letter (character) from it. But when I use substr() I only > properly get the first character, the rest is empty (""). What am I getting > wrong? > For example, I have this code: > >>>> > x <- "testing" > k <- nchar(x) > for (i in 1:k) { > y <- substr(x, i, 1) > print(y) > } > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.