[R] how to get a numeric vector?

John Fox j|ox @end|ng |rom mcm@@ter@c@
Mon Oct 5 00:59:51 CEST 2020


Dear vod vos,

On 2020-10-04 6:47 p.m., vod vos via R-help wrote:
> Hi,
> 
> a <- c(1, 4)
> b <- c(5, 8)
> 
> a:b
> 
> [1] 1 2 3 4 5
> Warning messages:
> 1: In a:b : numerical expression has 2 elements: only the first used
> 2: In a:b : numerical expression has 2 elements: only the first used
> 
> how to get:
> 
> c(1:5, 4:8)

The simplest way is c(1:5, 4:8) but I don't suppose that's what you 
really want. Perhaps the following is what you have in mind:

 > unlist(mapply(':', c(1, 4), c(5, 8), SIMPLIFY=FALSE))
  [1] 1 2 3 4 5 4 5 6 7 8

In your case, but not more generally,

 > as.vector(mapply(':', c(1, 4), c(5, 8)))
  [1] 1 2 3 4 5 4 5 6 7 8

also works.

I hope this helps,
  John

John Fox, Professor Emeritus
McMaster University
Hamilton, Ontario, Canada
web: https://socialsciences.mcmaster.ca/jfox/


> 
> Thanks.
> 
> ______________________________________________
> R-help using r-project.org 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.
>



More information about the R-help mailing list