Fwd: Concatenation?

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

Fwd: Concatenation?

Gerrit Eichner
[I forgot to keep it on the list.]

-------- Weitergeleitete Nachricht --------
Betreff: Re: [R] Concatenation?
Datum: Wed, 17 Feb 2021 22:14:13 +0100
Von: Gerrit Eichner <[hidden email]>
Organisation: JLU Gießen
An: Parkhurst, David <[hidden email]>

Hi David,

checkout, e. g., base-R's

paste0(site, depth)

or, if stored as columns of a matrix or data frame X, e.g.,

apply(X, 1, paste0)

or, e.g., tidyr's

unite.

  Hth  --  Gerrit

---------------------------------------------------------------------
Dr. Gerrit Eichner                   Mathematical Institute, Room 212
[hidden email]   Justus-Liebig-University Giessen
Tel: +49-(0)641-99-32104          Arndtstr. 2, 35392 Giessen, Germany
Fax: +49-(0)641-99-32109            http://www.uni-giessen.de/eichner
---------------------------------------------------------------------

Am 17.02.2021 um 22:09 schrieb Parkhurst, David:

> 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: Fwd: Concatenation?

Bert Gunter-2
Inline comment below.
Cheers,
Bert

Bert Gunter

"....
or, if stored as columns of a matrix or data frame X, e.g.,

>
> ##############

> apply(X, 1, paste0)
>
##############
"
No. paste() is vectorized.  apply() can be avoided:
> df  <- data.frame(X1 = letters[1:3],
                 X2 = 5:7,
                 X3 = LETTERS[1:3]
)
> df
  X1 X2 X3
1  a  5  A
2  b  6  B
3  c  7  C

> do.call(paste0, df)
[1] "a5A" "b6B" "c7C"



  Hth  --  Gerrit

>
> ---------------------------------------------------------------------
> Dr. Gerrit Eichner                   Mathematical Institute, Room 212
> [hidden email]   Justus-Liebig-University Giessen
> Tel: +49-(0)641-99-32104          Arndtstr. 2, 35392 Giessen, Germany
> Fax: +49-(0)641-99-32109            http://www.uni-giessen.de/eichner
> ---------------------------------------------------------------------
>
> Am 17.02.2021 um 22:09 schrieb Parkhurst, David:
> > 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.
>

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

Gerrit Eichner
Right, Bert, but not if X is "only"  matrix. ;-)

 > X <- cbind(X1 = letters[1:3],
            X2 = 5:7,
            X3 = LETTERS[1:3]
)
 > do.call(paste0, X)
Fehler in do.call(paste0, X) : das zweite Argument muss eine Liste sein

(Sorry, but my system is German. :-))

But, of course, then, e.g.,

do.call(paste0, data.frame(X))

would work.

  Best  --  Gerrit

---------------------------------------------------------------------
Dr. Gerrit Eichner                   Mathematical Institute, Room 212
[hidden email]   Justus-Liebig-University Giessen
Tel: +49-(0)641-99-32104          Arndtstr. 2, 35392 Giessen, Germany
http://www.uni-giessen.de/eichner
---------------------------------------------------------------------

Am 18.02.2021 um 17:08 schrieb Bert Gunter:

> Inline comment below.
> Cheers,
> Bert
>
> Bert Gunter
>
> "....
> or, if stored as columns of a matrix or data frame X, e.g.,
>
>
> ##############
>
>     apply(X, 1, paste0)
>
> ##############
> "
> No. paste() is vectorized. apply() can be avoided:
>> df  <- data.frame(X1 = letters[1:3],
>                   X2 = 5:7,
>                   X3 = LETTERS[1:3]
> )
>> df
>    X1 X2 X3
> 1  a  5  A
> 2  b  6  B
> 3  c  7  C
>
>> do.call(paste0, df)
> [1] "a5A" "b6B" "c7C"
>
>
>
>        Hth  --  Gerrit
>
>     ---------------------------------------------------------------------
>     Dr. Gerrit Eichner                   Mathematical Institute, Room 212
>     [hidden email]
>     <mailto:[hidden email]>
>       Justus-Liebig-University Giessen
>     Tel: +49-(0)641-99-32104          Arndtstr. 2, 35392 Giessen, Germany
>     Fax: +49-(0)641-99-32109 http://www.uni-giessen.de/eichner
>     <http://www.uni-giessen.de/eichner>
>     ---------------------------------------------------------------------
>
>     Am 17.02.2021 um 22:09 schrieb Parkhurst, David:
>      > 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] <mailto:[hidden email]> mailing list
>     -- To UNSUBSCRIBE and more, see
>      > https://stat.ethz.ch/mailman/listinfo/r-help
>     <https://stat.ethz.ch/mailman/listinfo/r-help>
>      > PLEASE do read the posting guide
>     http://www.R-project.org/posting-guide.html
>     <http://www.R-project.org/posting-guide.html>
>      > and provide commented, minimal, self-contained, reproducible code.
>      >
>
>     ______________________________________________
>     [hidden email] <mailto:[hidden email]> mailing list --
>     To UNSUBSCRIBE and more, see
>     https://stat.ethz.ch/mailman/listinfo/r-help
>     <https://stat.ethz.ch/mailman/listinfo/r-help>
>     PLEASE do read the posting guide
>     http://www.R-project.org/posting-guide.html
>     <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.