Santosh.Aryal at csiro.au
2016-Jan-05 06:19 UTC
[R] Merging two data files based on common dates
Hello there Pardon my ignorance but, I have two data files with different series of dates and x and y values. There are common dates in both files. For example>datafile1date1 xval 31/12/1982 20 1/01/1983 30 2/01/1983 40 3/01/1983 50 4/01/1983 60 5/01/1983 70 ... 01/01/2010 77 ... 31/12/1012 99>datafile2date2 yval 3/01/1983 0.4 4/01/1983 0.5 5/01/1983 0.6 .. 01/01/2010 88 All I want is a file/object that merges the two files with data for common dates to look like this. date yval xval 3/01/1983 0.4 50 4/01/1983 0.5 60 5/01/1983 0.6 70 .. 01/01/2010 88 77 I tried ' merge' and ' join' commands but somehow I have not been able to get that. Any help will be appreciated. Thank you. Best regards Santosh Aryal CSIRO Land and Water GPO Box 1666, Canberra ACT 2601 Ph: 02 6246 5963 Email: santosh.aryal at csiro.au<mailto:santosh.aryal at csiro.au> [[alternative HTML version deleted]]
Since the date columns have different names, you need to specify the by.x and by.y arguments to merge(). Other than that, it should work. If you need more help, please use dput() to provide some of your data, and include both the code you used and the error message or incorrect result you got (that is, a reproducible example). And also, post this information in plain text rather than HTML. Sarah On Tue, Jan 5, 2016 at 1:19 AM, <Santosh.Aryal at csiro.au> wrote:> Hello there > Pardon my ignorance but, I have two data files with different series of dates and x and y values. > There are common dates in both files. For example >>datafile1 > date1 xval > 31/12/1982 20 > 1/01/1983 30 > 2/01/1983 40 > 3/01/1983 50 > 4/01/1983 60 > 5/01/1983 70 > ... > 01/01/2010 77 > ... > 31/12/1012 99 > >>datafile2 > date2 yval > 3/01/1983 0.4 > 4/01/1983 0.5 > 5/01/1983 0.6 > .. > 01/01/2010 88 > > All I want is a file/object that merges the two files with data for common dates to look like this. > date yval xval > 3/01/1983 0.4 50 > 4/01/1983 0.5 60 > 5/01/1983 0.6 70 > .. > 01/01/2010 88 77 > > I tried ' merge' and ' join' commands but somehow I have not been able to get that. Any help will be appreciated. > Thank you. > > > Best regards > > Santosh Aryal > CSIRO Land and Water > GPO Box 1666, Canberra ACT 2601 > Ph: 02 6246 5963 > Email: santosh.aryal at csiro.au<mailto:santosh.aryal at csiro.au> > > >-- Sarah Goslee http://www.numberwright.com
You did not show the structure of your datasets (with, e.g., dump(c("datafile1","datafile2"),file=stdout())) nor what your call to merge() was. However, it may be that you did not use the by.x and by.y arguments to merge() to specify which columns to match. txt1 <- "date1 xval 31/12/1982 20 1/01/1983 30 2/01/1983 40 3/01/1983 50 4/01/1983 60 5/01/1983 70" txt2 <- "date2 yval 3/01/1983 0.4 4/01/1983 0.5 5/01/1983 0.6" df1 <- read.table(text=txt1, header=TRUE) df2 <- read.table(text=txt2, header=TRUE) merge(x=df2, y=df1, by.x="date2", by.y="date1") # date2 yval xval # 1 3/01/1983 0.4 50 # 2 4/01/1983 0.5 60 # 3 5/01/1983 0.6 70 Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Jan 4, 2016 at 10:19 PM, <Santosh.Aryal at csiro.au> wrote:> Hello there > Pardon my ignorance but, I have two data files with different series of > dates and x and y values. > There are common dates in both files. For example > >datafile1 > date1 xval > 31/12/1982 20 > 1/01/1983 30 > 2/01/1983 40 > 3/01/1983 50 > 4/01/1983 60 > 5/01/1983 70 > ... > 01/01/2010 77 > ... > 31/12/1012 99 > > >datafile2 > date2 yval > 3/01/1983 0.4 > 4/01/1983 0.5 > 5/01/1983 0.6 > .. > 01/01/2010 88 > > All I want is a file/object that merges the two files with data for common > dates to look like this. > date yval xval > 3/01/1983 0.4 50 > 4/01/1983 0.5 60 > 5/01/1983 0.6 70 > .. > 01/01/2010 88 77 > > I tried ' merge' and ' join' commands but somehow I have not been able to > get that. Any help will be appreciated. > Thank you. > > > Best regards > > Santosh Aryal > CSIRO Land and Water > GPO Box 1666, Canberra ACT 2601 > Ph: 02 6246 5963 > Email: santosh.aryal at csiro.au<mailto:santosh.aryal at csiro.au> > > > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >[[alternative HTML version deleted]]