Concatenation?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Concatenation?

David Parkhurst
If I have a vector of site abbreviations and a vector of depths in those water bodies, is there a simple way in R to combine them to make a third vector?
Examples:

site depth desired
MU 0 MU0
MU 1 MU1
MU 2 MU2
MC 0 MC0
MC 1 MC1
MC 2 MC2

The dataset has many more lines than this.  I can see how to do this with lots of if statements, but does R have magic that can make it happen easily?  I guess this would be called concatenation. 

______________________________________________
[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: Concatenation?

Eric Berger
desired <- paste(site,depth,sep="")

On Wed, Feb 17, 2021 at 11:10 PM Parkhurst, David <[hidden email]>
wrote:

> If I have a vector of site abbreviations and a vector of depths in those
> water bodies, is there a simple way in R to combine them to make a third
> vector?
> Examples:
>
> site    depth           desired
> MU      0               MU0
> MU      1               MU1
> MU      2               MU2
> MC      0               MC0
> MC      1               MC1
> MC      2               MC2
>
> The dataset has many more lines than this.  I can see how to do this with
> lots of if statements, but does R have magic that can make it happen
> easily?  I guess this would be called concatenation.
>
> ______________________________________________
> [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.
Reply | Threaded
Open this post in threaded view
|

Re: [External] Concatenation?

Richard M. Heiberger
In reply to this post by David Parkhurst
> paste(c("A","B","C"), c(1,2,3), sep="")
[1] "A1" "B2" "C3"

in your example
paste(site, depth, sep="")

________________________________________
From: R-help <[hidden email]> on behalf of Parkhurst, David <[hidden email]>
Sent: Wednesday, February 17, 2021 4:09 PM
To: [hidden email]
Subject: [External] [R] Concatenation?

If I have a vector of site abbreviations and a vector of depths in those water bodies, is there a simple way in R to combine them to make a third vector?
Examples:

site    depth           desired
MU      0               MU0
MU      1               MU1
MU      2               MU2
MC      0               MC0
MC      1               MC1
MC      2               MC2

The dataset has many more lines than this.  I can see how to do this with lots of if statements, but does R have magic that can make it happen easily?  I guess this would be called concatenation.

______________________________________________
[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: Concatenation?

pikappa.devel
In reply to this post by David Parkhurst
How about paste?
site <- c('MU','MU','MU','MC','MC','MC')depth <-
c(0,1,2,0,1,2)paste(site, depth, sep="")
result:
[1] "MU0" "MU1" "MU2" "MC0" "MC1" "MC2"
On Wed, 2021-02-17 at 21:09 +0000, Parkhurst, David wrote:

> If I have a vector of site abbreviations and a vector of depths in
> those water bodies, is there a simple way in R to combine them to
> make a third vector?Examples:
> site depth desiredMU 0 MU0MU 1
> MU1MU 2 MU2MC 0 MC0MC 1
> MC1MC 2 MC2
> The dataset has many more lines than this.  I can see how to do this
> with lots of if statements, but does R have magic that can make it
> happen easily?  I guess this would be called concatenation.
> [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.
Reply | Threaded
Open this post in threaded view
|

Re: [External] Concatenation?

David Parkhurst
In reply to this post by Richard M. Heiberger
Thank you.

From: Richard M. Heiberger <[hidden email]>
Date: Wednesday, February 17, 2021 at 4:21 PM
To: Parkhurst, David <[hidden email]>, [hidden email] <[hidden email]>
Subject: Re: [External] [R] Concatenation?
> paste(c("A","B","C"), c(1,2,3), sep="")
[1] "A1" "B2" "C3"

in your example
paste(site, depth, sep="")

________________________________________
From: R-help <[hidden email]> on behalf of Parkhurst, David <[hidden email]>
Sent: Wednesday, February 17, 2021 4:09 PM
To: [hidden email]
Subject: [External] [R] Concatenation?

If I have a vector of site abbreviations and a vector of depths in those water bodies, is there a simple way in R to combine them to make a third vector?
Examples:

site    depth           desired
MU      0               MU0
MU      1               MU1
MU      2               MU2
MC      0               MC0
MC      1               MC1
MC      2               MC2

The dataset has many more lines than this.  I can see how to do this with lots of if statements, but does R have magic that can make it happen easily?  I guess this would be called concatenation.

______________________________________________
[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.
Reply | Threaded
Open this post in threaded view
|

Re: [External] Concatenation?

Bert Gunter-2
... or simply
> paste0(c("A","B","C"), c(1,2,3))
[1] "A1" "B2" "C3"

See ?paste for other useful options.

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Wed, Feb 17, 2021 at 1:26 PM Parkhurst, David <[hidden email]>
wrote:

> Thank you.
>
> From: Richard M. Heiberger <[hidden email]>
> Date: Wednesday, February 17, 2021 at 4:21 PM
> To: Parkhurst, David <[hidden email]>, [hidden email] <
> [hidden email]>
> Subject: Re: [External] [R] Concatenation?
> > paste(c("A","B","C"), c(1,2,3), sep="")
> [1] "A1" "B2" "C3"
>
> in your example
> paste(site, depth, sep="")
>
> ________________________________________
> From: R-help <[hidden email]> on behalf of Parkhurst, David
> <[hidden email]>
> Sent: Wednesday, February 17, 2021 4:09 PM
> To: [hidden email]
> Subject: [External] [R] Concatenation?
>
> If I have a vector of site abbreviations and a vector of depths in those
> water bodies, is there a simple way in R to combine them to make a third
> vector?
> Examples:
>
> site    depth           desired
> MU      0               MU0
> MU      1               MU1
> MU      2               MU2
> MC      0               MC0
> MC      1               MC1
> MC      2               MC2
>
> The dataset has many more lines than this.  I can see how to do this with
> lots of if statements, but does R have magic that can make it happen
> easily?  I guess this would be called concatenation.
>
> ______________________________________________
> [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.
>

        [[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.