[R] Combining the values of two variables into one
    Marc Schwartz 
    marc_schwartz at me.com
       
    Sat Oct 23 00:28:26 CEST 2010
    
    
  
On Oct 22, 2010, at 4:14 PM, Marc Schwartz wrote:
> On Oct 22, 2010, at 4:00 PM, David Herzberg wrote:
> 
>> I start with:
>> 
>> v1<-c(1,3,5,7)
>> v2<-c(2,4,6,8)
>> 
>> And I want to end up with:
>> 
>> v3<-c(12,34,56,78)
>> 
>> How do I get there?
>> 
>> Thanks,
> 
> 
>> v1*10 + v2
> [1] 12 34 56 78
> 
David,
It occurs to me that my solution may not be correct, depending upon what your real source data are.
For example:
v1 <- c(12, 34)
v2 <- (56, 78)
> v1*10 + v2
[1] 176 418
Does not of course get you:
  c(1256, 3478)
If that is what you actually want in that scenario, use:
> as.numeric(paste(v1, v2, sep = ""))
[1] 1256 3478
That also works for your original data:
v1<-c(1,3,5,7)
v2<-c(2,4,6,8)
> as.numeric(paste(v1, v2, sep = ""))
[1] 12 34 56 78
See ?paste, which returns a character vector, which you then coerce to numeric.
HTH,
Marc
    
    
More information about the R-help
mailing list