Re: [R] Merging two columns of a data frame

From: Scott Chamberlain <scttchamberlain4_at_gmail.com>
Date: Thu, 28 Apr 2011 22:31:19 -0500

what about something like:

dat <- data.frame(var1 = c("a","b"), var2 = c("c","d")) dat$var3 <- sapply(dat, function(x) paste(dat$var1,dat$var2,sep=""))[,1]

Scott
On Thursday, April 28, 2011 at 10:22 AM, Abraham Mathew wrote: I've kind of solved the issue.
>
> dat$Time <- paste(dat[,2], dat[,1], sep=" ")
> head(dat)
> dat[,c(1,2)] <- NA
> head(dat)
>
> > head(dat)
> Year Month Number Time
> 1 NA NA 0 Jan 2002
> 2 NA NA 0 Feb 2002
> 3 NA NA 0 March 2002
> 4 NA NA 1 April 2002
> 5 NA NA 0 May 2002
> 6 NA NA 0 June 2002
>
> unfortunately, can I delete the Year and Month Columns.
> Once that's done, I can reconfigure the columns
>
> Abraham
>
> On Thu, Apr 28, 2011 at 11:00 AM, Abraham Mathew <abmathewks_at_gmail.com>wrote:
>
> >
> > Hi folks, I have a simple question that I just can't solve.
> >
> > I'm trying to merge two columns in my data frame.
> >
> > > sessionInfo()
> > R version 2.13.0 (2011-04-13)
> > Platform: i686-pc-linux-gnu (32-bit)
> >
> > > head(dat)
> > Year Month Number
> > 2002 Jan 0
> > 2002 Feb 0
> > 2002 March 0
> > 2002 April 1
> > 2002 May 0
> > 2002 June 0
> >
> > I tried to do the following, but it doesn't work.
> >
> > dat$Time <- merge(dat[,1], dat[,2])
> > head(dat)
> >
> >
> > Help, Abraham.
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>

        [[alternative HTML version deleted]]



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. Received on Fri 29 Apr 2011 - 03:34:44 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 29 Apr 2011 - 03:40:33 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive