Quantcast

substr not by position but by symbol

classic Classic list List threaded Threaded
3 messages Options
YN
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

substr not by position but by symbol

YN
Hi all,

One of my variables looks like this:

.7_-.3_-.2_.9

And this is a character variable. I made this by combining four different
number like .7, -.3, -.2, and .9 using paste function.
Now, I want to go back to original format from this one combined character
variable. For instance, I want to extract 3rd number, -.2, from this
character.

I know I can use substr function, but each position is not fixed because
sometimes each number has negative sign. Instead, it would be good if there
is any extracting function which can extract character not by position but
by specific symbol, in my case symbol '_'. Is there any similar function
for doing in R?

Thanks!

YN

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: substr not by position but by symbol

Ista Zahn
Hi YN,

I use strsplit for this:

x <- ".7_-.3_-.2_.9"

> strsplit(x, split = "_")
[[1]]
[1] ".7"  "-.3" "-.2" ".9"

> strsplit(x, split = "_")[[1]][3]
[1] "-.2"


Best,
Ista
On Mon, May 7, 2012 at 9:54 AM, YN Kim <[hidden email]> wrote:

> Hi all,
>
> One of my variables looks like this:
>
> .7_-.3_-.2_.9
>
> And this is a character variable. I made this by combining four different
> number like .7, -.3, -.2, and .9 using paste function.
> Now, I want to go back to original format from this one combined character
> variable. For instance, I want to extract 3rd number, -.2, from this
> character.
>
> I know I can use substr function, but each position is not fixed because
> sometimes each number has negative sign. Instead, it would be good if there
> is any extracting function which can extract character not by position but
> by specific symbol, in my case symbol '_'. Is there any similar function
> for doing in R?
>
> Thanks!
>
> YN
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: substr not by position but by symbol

Rui Barradas
In reply to this post by YN
Hello,

YN wrote
Hi all,

One of my variables looks like this:

.7_-.3_-.2_.9

And this is a character variable. I made this by combining four different
number like .7, -.3, -.2, and .9 using paste function.
Now, I want to go back to original format from this one combined character
variable. For instance, I want to extract 3rd number, -.2, from this
character.

I know I can use substr function, but each position is not fixed because
sometimes each number has negative sign. Instead, it would be good if there
is any extracting function which can extract character not by position but
by specific symbol, in my case symbol '_'. Is there any similar function
for doing in R?

Thanks!

YN

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Yes there is.

x <- ".7_-.3_-.2_.9"
strsplit(x, "_")

# To get the numbers, not character vectors
as.numeric(unlist(strsplit(x, "_")))
# Or
sapply(strsplit(x, "_"), as.numeric)

Hope this helps,

Rui Barradas
Loading...