substr gives empty output

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

substr gives empty output

Luigi
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]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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 | Threaded
Open this post in threaded view
|

Re: [FORGED] substr gives empty output

Rolf Turner

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

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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 | Threaded
Open this post in threaded view
|

Re: substr gives empty output

Ted Harding
In reply to this post by Luigi
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 page
  substr(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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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 | Threaded
Open this post in threaded view
|

Re: substr gives empty output

Ek Esawi
In reply to this post by Luigi
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
<[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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 | Threaded
Open this post in threaded view
|

Re: substr gives empty output

Howard, Tim G (DEC)
In reply to this post by Luigi
In

 y <- substr(x, i, 1)

your third integer needs to be the location not the number of digits, so change it to

 y <- substr(x, i, i)

and you should get what you want.
Cheers,
Tim

> Date: Sun, 21 Jan 2018 10:50:31 -0500
> From: Ek Esawi <[hidden email]>
> To: Luigi Marongiu <[hidden email]>, [hidden email]
> Subject: Re: [R] substr gives empty output
> Message-ID:
>         <CA+ZkTxubYDSZ3iqsg_=be9HBA2_3-TE95=mXbh4atvG-
> [hidden email]>
> Content-Type: text/plain; charset="UTF-8"
>
> 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 <[hidden email]>
> 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]]
> >
> > ______________________________________________
> > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > 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.
>
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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 | Threaded
Open this post in threaded view
|

Re: substr gives empty output

David Carlson
There is also a way to do this without a loop:

> strsplit(x, "")
[[1]]
[1] "t" "e" "s" "t" "i" "n" "g"
# Or if you just want the vector
> strsplit(x, "")[[1]]
[1] "t" "e" "s" "t" "i" "n" "g"

----------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77843-4352


-----Original Message-----
From: R-help [mailto:[hidden email]] On Behalf Of Howard, Tim G (DEC)
Sent: Monday, January 22, 2018 6:58 AM
To: [hidden email]; Luigi Marongiu <[hidden email]>
Subject: Re: [R] substr gives empty output

In

 y <- substr(x, i, 1)

your third integer needs to be the location not the number of digits, so change it to

 y <- substr(x, i, i)

and you should get what you want.
Cheers,
Tim

> Date: Sun, 21 Jan 2018 10:50:31 -0500
> From: Ek Esawi <[hidden email]>
> To: Luigi Marongiu <[hidden email]>, [hidden email]
> Subject: Re: [R] substr gives empty output
> Message-ID:
>         <CA+ZkTxubYDSZ3iqsg_=be9HBA2_3-TE95=mXbh4atvG-
> [hidden email]>
> Content-Type: text/plain; charset="UTF-8"
>
> 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
> <[hidden email]>
> 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]]
> >
> > ______________________________________________
> > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > 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.
>
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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 | Threaded
Open this post in threaded view
|

Re: substr gives empty output

Luigi
In reply to this post by Howard, Tim G (DEC)
Thank you, I got it, now it works good

On Mon, Jan 22, 2018 at 1:58 PM, Howard, Tim G (DEC) <[hidden email]>
wrote:

> In
>
>  y <- substr(x, i, 1)
>
> your third integer needs to be the location not the number of digits, so
> change it to
>
>  y <- substr(x, i, i)
>
> and you should get what you want.
> Cheers,
> Tim
>
> > Date: Sun, 21 Jan 2018 10:50:31 -0500
> > From: Ek Esawi <[hidden email]>
> > To: Luigi Marongiu <[hidden email]>, [hidden email]
> > Subject: Re: [R] substr gives empty output
> > Message-ID:
> >         <CA+ZkTxubYDSZ3iqsg_=be9HBA2_3-TE95=mXbh4atvG-
> > [hidden email]>
> > Content-Type: text/plain; charset="UTF-8"
> >
> > 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 <
> [hidden email]>
> > 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]]
> > >
> > > ______________________________________________
> > > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > > 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.
> >
> >
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.