[R] merging columns

Prof Brian Ripley ripley at stats.ox.ac.uk
Sun Nov 4 15:03:07 CET 2001


On Sun, 4 Nov 2001, Antonio Olinto wrote:

> Dear R-list members
>
> I’m trying, without success, to create new a column (YQ) into a data.frame
> from another two columns (Y and Q):
> Y       Q   YQ
> 1998  1    98-1
>
> I’ve tried several commands. cbind, for instance, gives me "98" "-" "1" and
> not 98-1.
>
> Could someone help me? Many thanks.
>
> Antonio Olinto
>
>
> Example:
>
> > data.dat <- data.frame(rep(1998:2001,c(4,4,4,4)),rep(1:4,4))
> > names(data.dat)<- c("Y","Q")
> > attach(data.dat)
> > cbind(substring(Y,3,4),"-",Q)
>                      Q
>  [1,] "98" "-" "1"
>  [2,] "98" "-" "2"
>  [3,] "98" "-" "3"
>  [4,] "98" "-" "4"
>  [5,] "99" "-" "1"
>  [6,] ...

data.dat$YQ <- paste(substring(Y,3,4), Q, sep="-")
data.dat
      Y Q   YQ
1  1998 1 98-1
2  1998 2 98-2
3  1998 3 98-3
4  1998 4 98-4
5  1999 1 99-1
6  1999 2 99-2
...

>
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list