Combine by columns a vector with another vector that is constant across rows

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

Combine by columns a vector with another vector that is constant across rows

Viechtbauer Wolfgang (SP)
Hi All,

I have one vector that I want to combine with another vector and that other vector should be the same for every row in the combined matrix. This obviously does not work:

vec <- c(2,4,3)
cbind(1:5, vec)

This does, but requires me to specify the correct value for 'n' in replicate():

cbind(1:5, t(replicate(5, vec)))

Other ways that do not require this are:

t(sapply(1:5, function(x) c(x, vec)))
do.call(rbind, lapply(1:5, function(x) c(x, vec)))
t(mapply(c, 1:5, MoreArgs=list(vec)))

I wonder if there is a simpler / more efficient way of doing this.

Best,
Wolfgang

______________________________________________
[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: Combine by columns a vector with another vector that is constant across rows

PIKAL Petr
Hi

If you put 1:5 vector to x you could do

cbind(x,t(replicate(length(x), vec)))

Cheers
Petr
Osobní údaje: Informace o zpracování a ochraně osobních údajů obchodních partnerů PRECHEZA a.s. jsou zveřejněny na: https://www.precheza.cz/zasady-ochrany-osobnich-udaju/ | Information about processing and protection of business partner's personal data are available on website: https://www.precheza.cz/en/personal-data-protection-principles/
Důvěrnost: Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a podléhají tomuto právně závaznému prohlášení o vyloučení odpovědnosti: https://www.precheza.cz/01-dovetek/ | This email and any documents attached to it may be confidential and are subject to the legally binding disclaimer: https://www.precheza.cz/en/01-disclaimer/

> -----Original Message-----
> From: R-help [mailto:[hidden email]] On Behalf Of Viechtbauer,
> Wolfgang (SP)
> Sent: Tuesday, July 3, 2018 3:29 PM
> To: [hidden email]
> Subject: [R] Combine by columns a vector with another vector that is constant
> across rows
>
> Hi All,
>
> I have one vector that I want to combine with another vector and that other
> vector should be the same for every row in the combined matrix. This obviously
> does not work:
>
> vec <- c(2,4,3)
> cbind(1:5, vec)
>
> This does, but requires me to specify the correct value for 'n' in replicate():
>
> cbind(1:5, t(replicate(5, vec)))
>
> Other ways that do not require this are:
>
> t(sapply(1:5, function(x) c(x, vec)))
> do.call(rbind, lapply(1:5, function(x) c(x, vec))) t(mapply(c, 1:5,
> MoreArgs=list(vec)))
>
> I wonder if there is a simpler / more efficient way of doing this.
>
> Best,
> Wolfgang
>
> ______________________________________________
> [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: Combine by columns a vector with another vector that is constant across rows

Gabor Grothendieck
In reply to this post by Viechtbauer Wolfgang (SP)
Try Reduce:

  Reduce(cbind, vec, 1:5)

On Tue, Jul 3, 2018 at 9:28 AM, Viechtbauer, Wolfgang (SP)
<[hidden email]> wrote:

> Hi All,
>
> I have one vector that I want to combine with another vector and that other vector should be the same for every row in the combined matrix. This obviously does not work:
>
> vec <- c(2,4,3)
> cbind(1:5, vec)
>
> This does, but requires me to specify the correct value for 'n' in replicate():
>
> cbind(1:5, t(replicate(5, vec)))
>
> Other ways that do not require this are:
>
> t(sapply(1:5, function(x) c(x, vec)))
> do.call(rbind, lapply(1:5, function(x) c(x, vec)))
> t(mapply(c, 1:5, MoreArgs=list(vec)))
>
> I wonder if there is a simpler / more efficient way of doing this.
>
> Best,
> Wolfgang
>
> ______________________________________________
> [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.



--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

______________________________________________
[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: Combine by columns a vector with another vector that is constant across rows

Jeff Newmiller
In reply to this post by Viechtbauer Wolfgang (SP)
Gabor's solution seems to optimize 'simpler'.

More efficient is to learn that in R a vector is not a matrix, but a matrix is just an ornamented vector.

fastWolfgang <- function( v, vec ) {
  matrix( c( v, rep( vec, length( v ) ) )
         , now = length( v ) )
}

On July 3, 2018 6:28:45 AM PDT, "Viechtbauer, Wolfgang (SP)" <[hidden email]> wrote:

>Hi All,
>
>I have one vector that I want to combine with another vector and that
>other vector should be the same for every row in the combined matrix.
>This obviously does not work:
>
>vec <- c(2,4,3)
>cbind(1:5, vec)
>
>This does, but requires me to specify the correct value for 'n' in
>replicate():
>
>cbind(1:5, t(replicate(5, vec)))
>
>Other ways that do not require this are:
>
>t(sapply(1:5, function(x) c(x, vec)))
>do.call(rbind, lapply(1:5, function(x) c(x, vec)))
>t(mapply(c, 1:5, MoreArgs=list(vec)))
>
>I wonder if there is a simpler / more efficient way of doing this.
>
>Best,
>Wolfgang
>
>______________________________________________
>[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.

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[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: Combine by columns a vector with another vector that is constant across rows

Gabor Grothendieck
In reply to this post by Gabor Grothendieck
or this variation if you don't want the first column to be named init:

 Reduce(cbind2, vec, 1:5)

On Tue, Jul 3, 2018 at 10:46 AM, Gabor Grothendieck
<[hidden email]> wrote:

> Try Reduce:
>
>   Reduce(cbind, vec, 1:5)
>
> On Tue, Jul 3, 2018 at 9:28 AM, Viechtbauer, Wolfgang (SP)
> <[hidden email]> wrote:
>> Hi All,
>>
>> I have one vector that I want to combine with another vector and that other vector should be the same for every row in the combined matrix. This obviously does not work:
>>
>> vec <- c(2,4,3)
>> cbind(1:5, vec)
>>
>> This does, but requires me to specify the correct value for 'n' in replicate():
>>
>> cbind(1:5, t(replicate(5, vec)))
>>
>> Other ways that do not require this are:
>>
>> t(sapply(1:5, function(x) c(x, vec)))
>> do.call(rbind, lapply(1:5, function(x) c(x, vec)))
>> t(mapply(c, 1:5, MoreArgs=list(vec)))
>>
>> I wonder if there is a simpler / more efficient way of doing this.
>>
>> Best,
>> Wolfgang
>>
>> ______________________________________________
>> [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.
>
>
>
> --
> Statistics & Software Consulting
> GKX Group, GKX Associates Inc.
> tel: 1-877-GKX-GROUP
> email: ggrothendieck at gmail.com



--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

______________________________________________
[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: Combine by columns a vector with another vector that is constant across rows

Jeff Newmiller
In reply to this post by Jeff Newmiller
Sorry trying again...

fastWolfgang <- function( v, vec ) {
  matrix( c( v, rep( vec, each = length( v ) ) )
         , nrow = length( v ) )
}

On July 3, 2018 8:21:47 AM PDT, Jeff Newmiller <[hidden email]> wrote:

>Gabor's solution seems to optimize 'simpler'.
>
>More efficient is to learn that in R a vector is not a matrix, but a
>matrix is just an ornamented vector.
>
>fastWolfgang <- function( v, vec ) {
>  matrix( c( v, rep( vec, length( v ) ) )
>         , now = length( v ) )
>}
>
>On July 3, 2018 6:28:45 AM PDT, "Viechtbauer, Wolfgang (SP)"
><[hidden email]> wrote:
>>Hi All,
>>
>>I have one vector that I want to combine with another vector and that
>>other vector should be the same for every row in the combined matrix.
>>This obviously does not work:
>>
>>vec <- c(2,4,3)
>>cbind(1:5, vec)
>>
>>This does, but requires me to specify the correct value for 'n' in
>>replicate():
>>
>>cbind(1:5, t(replicate(5, vec)))
>>
>>Other ways that do not require this are:
>>
>>t(sapply(1:5, function(x) c(x, vec)))
>>do.call(rbind, lapply(1:5, function(x) c(x, vec)))
>>t(mapply(c, 1:5, MoreArgs=list(vec)))
>>
>>I wonder if there is a simpler / more efficient way of doing this.
>>
>>Best,
>>Wolfgang
>>
>>______________________________________________
>>[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.

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[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: Combine by columns a vector with another vector that is constant across rows

Viechtbauer Wolfgang (SP)
Thanks for all of the suggestions. I did some benchmarking:

library(microbenchmark)

x <- 1:5
vec <- c(2,4,3)

fastWolfgang <- function(v, vec)
   matrix(c(v, rep(vec, each = length(v))), nrow = length(v))

microbenchmark(cbind(x, t(replicate(length(x), vec))),
               t(sapply(x, function(x) c(x, vec))),
               do.call(rbind, lapply(x, function(x) c(x, vec))),
               t(mapply(c, x, MoreArgs=list(vec))),
               Reduce(cbind, vec, x),
               Reduce(cbind2, vec, x),
               fastWolfgang(x, vec), times=10000L)

Jeff's approach is fastest, but Gabor's Reduce(cbind, vec, x) is close (and I really like its simplicity); and very similar to the do.call() approach.

Interestingly, for larger vectors, such as:

x <- 1:50
vec <- sample(1:100, 200, replace=TRUE)

the do.call() approach is the fastest.

Best,
Wolfgang

>-----Original Message-----
>From: Jeff Newmiller [mailto:[hidden email]]
>Sent: Tuesday, 03 July, 2018 17:48
>To: [hidden email]; Viechtbauer, Wolfgang (SP); r-help@r-
>project.org
>Subject: Re: [R] Combine by columns a vector with another vector that is
>constant across rows
>
>Sorry trying again...
>
>fastWolfgang <- function( v, vec ) {
>  matrix( c( v, rep( vec, each = length( v ) ) )
>         , nrow = length( v ) )
>}
>
>On July 3, 2018 8:21:47 AM PDT, Jeff Newmiller <[hidden email]>
>wrote:
>>Gabor's solution seems to optimize 'simpler'.
>>
>>More efficient is to learn that in R a vector is not a matrix, but a
>>matrix is just an ornamented vector.
>>
>>fastWolfgang <- function( v, vec ) {
>>  matrix( c( v, rep( vec, length( v ) ) )
>>         , now = length( v ) )
>>}
>>
>>On July 3, 2018 6:28:45 AM PDT, "Viechtbauer, Wolfgang (SP)"
>><[hidden email]> wrote:
>>>Hi All,
>>>
>>>I have one vector that I want to combine with another vector and that
>>>other vector should be the same for every row in the combined matrix.
>>>This obviously does not work:
>>>
>>>vec <- c(2,4,3)
>>>cbind(1:5, vec)
>>>
>>>This does, but requires me to specify the correct value for 'n' in
>>>replicate():
>>>
>>>cbind(1:5, t(replicate(5, vec)))
>>>
>>>Other ways that do not require this are:
>>>
>>>t(sapply(1:5, function(x) c(x, vec)))
>>>do.call(rbind, lapply(1:5, function(x) c(x, vec)))
>>>t(mapply(c, 1:5, MoreArgs=list(vec)))
>>>
>>>I wonder if there is a simpler / more efficient way of doing this.
>>>
>>>Best,
>>>Wolfgang
______________________________________________
[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: Combine by columns a vector with another vector that is constant across rows

Eric Berger
For what it's worth, for larger vectors, and following on from your
observation that the do.call() approach is faster, the following provides
some modest additional speedup:

 cbind(x,t(do.call(cbind, lapply(x, function(y) vec))))

Rgds,
Eric


On Tue, Jul 3, 2018 at 8:12 PM, Viechtbauer, Wolfgang (SP) <
[hidden email]> wrote:

> Thanks for all of the suggestions. I did some benchmarking:
>
> library(microbenchmark)
>
> x <- 1:5
> vec <- c(2,4,3)
>
> fastWolfgang <- function(v, vec)
>    matrix(c(v, rep(vec, each = length(v))), nrow = length(v))
>
> microbenchmark(cbind(x, t(replicate(length(x), vec))),
>                t(sapply(x, function(x) c(x, vec))),
>                do.call(rbind, lapply(x, function(x) c(x, vec))),
>                t(mapply(c, x, MoreArgs=list(vec))),
>                Reduce(cbind, vec, x),
>                Reduce(cbind2, vec, x),
>                fastWolfgang(x, vec), times=10000L)
>
> Jeff's approach is fastest, but Gabor's Reduce(cbind, vec, x) is close
> (and I really like its simplicity); and very similar to the do.call()
> approach.
>
> Interestingly, for larger vectors, such as:
>
> x <- 1:50
> vec <- sample(1:100, 200, replace=TRUE)
>
> the do.call() approach is the fastest.
>
> Best,
> Wolfgang
>
> >-----Original Message-----
> >From: Jeff Newmiller [mailto:[hidden email]]
> >Sent: Tuesday, 03 July, 2018 17:48
> >To: [hidden email]; Viechtbauer, Wolfgang (SP); r-help@r-
> >project.org
> >Subject: Re: [R] Combine by columns a vector with another vector that is
> >constant across rows
> >
> >Sorry trying again...
> >
> >fastWolfgang <- function( v, vec ) {
> >  matrix( c( v, rep( vec, each = length( v ) ) )
> >         , nrow = length( v ) )
> >}
> >
> >On July 3, 2018 8:21:47 AM PDT, Jeff Newmiller <[hidden email]>
> >wrote:
> >>Gabor's solution seems to optimize 'simpler'.
> >>
> >>More efficient is to learn that in R a vector is not a matrix, but a
> >>matrix is just an ornamented vector.
> >>
> >>fastWolfgang <- function( v, vec ) {
> >>  matrix( c( v, rep( vec, length( v ) ) )
> >>         , now = length( v ) )
> >>}
> >>
> >>On July 3, 2018 6:28:45 AM PDT, "Viechtbauer, Wolfgang (SP)"
> >><[hidden email]> wrote:
> >>>Hi All,
> >>>
> >>>I have one vector that I want to combine with another vector and that
> >>>other vector should be the same for every row in the combined matrix.
> >>>This obviously does not work:
> >>>
> >>>vec <- c(2,4,3)
> >>>cbind(1:5, vec)
> >>>
> >>>This does, but requires me to specify the correct value for 'n' in
> >>>replicate():
> >>>
> >>>cbind(1:5, t(replicate(5, vec)))
> >>>
> >>>Other ways that do not require this are:
> >>>
> >>>t(sapply(1:5, function(x) c(x, vec)))
> >>>do.call(rbind, lapply(1:5, function(x) c(x, vec)))
> >>>t(mapply(c, 1:5, MoreArgs=list(vec)))
> >>>
> >>>I wonder if there is a simpler / more efficient way of doing this.
> >>>
> >>>Best,
> >>>Wolfgang
> ______________________________________________
> [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.