Hi, I'm trying to concatenate values from two columns in a data frame. For example, I have the following data.frame: C1 C2 C3 C4 C5 A B *F C* Q G H *I J* T K D *R S* E P L *M N* O I'd like to concatenate text from columns C3 and C4, to yield either a list or vector, like so: NewCol FC IJ RS MN Is this feasible in R? Thanks!
Dimitris Rizopoulos
2009-Feb-02 14:54 UTC
[R] concatenating 2 text columns in a data.frame
yes, try this:
dat <- read.table(textConnection(
"C1 C2 C3 C4 C5
A B F C Q
G H I J T
K D R S E
P L M N O"
), header = TRUE)
closeAllConnections()
dat$NewCol <- do.call(paste, c(dat[c("C3", "C4")], sep =
""))
dat
I hope it helps.
Best,
Dimitris
Shaun Grannis wrote:> Hi,
>
> I'm trying to concatenate values from two columns in a data frame. For
> example, I have the following data.frame:
>
> C1 C2 C3 C4 C5
> A B *F C* Q
> G H *I J* T
> K D *R S* E
> P L *M N* O
>
> I'd like to concatenate text from columns C3 and C4, to yield either a
> list or vector, like so:
>
> NewCol
> FC
> IJ
> RS
> MN
>
> Is this feasible in R?
>
> Thanks!
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>
--
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus Medical Center
Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014
On Mon, Feb 02, 2009 at 09:44:28AM -0500, Shaun Grannis wrote:> I'm trying to concatenate values from two columns in a data frame. For > example, I have the following data.frame: > > C1 C2 C3 C4 C5 > A B *F C* Q > G H *I J* T > K D *R S* E > P L *M N* O > > I'd like to concatenate text from columns C3 and C4, to yield either a > list or vector, like so: > > NewCol > FC > IJ > RS > MNassuming your data frame is named foo: foo$NewCol <- paste(foo$C3, foo$C4, sep='') cu Philipp -- Dr. Philipp Pagel Lehrstuhl f?r Genomorientierte Bioinformatik Technische Universit?t M?nchen Wissenschaftszentrum Weihenstephan 85350 Freising, Germany http://mips.gsf.de/staff/pagel