[R] merging two data sets with no column header
David Winsemius
dwinsemius at comcast.net
Tue Jun 3 04:12:08 CEST 2008
kayj <kjaja27 at yahoo.com> wrote in news:17613296.post at talk.nabble.com:
>
> I have two data sets data1 and data2 with no column names( No
> header). The first coluimn in both data sets represents the case ID.
> How can I tell R to merge the two data sets by the case ID if there
> is no header? Also, In data1 I have more cases and I would like the
> result of the merge to have all the cases in both data1 and data2.
> there may be some duplicate cases.
If you bring the data into a dataframe, default variable names will be
assigned. merge would be the function to use but you will need to
decide if its default behavior is satisfactory.
See whether these examples are illuminating:
vv <- data.frame(V1=1:10, V2=letters[1:10])
# read.table("data1.txt", header=FALSE) would produce a data.frame
# with # first variable, V1
# However, merge would use all the column names the are shared unless
# told to not do so. So give the other columns different names:
vv2 <- data.frame(V1=5:14, V8=letters[5:14])
merge(vv,vv2)
merge(vv,vv2,all=TRUE)
# create a duplicate
vv[8,1] <- 7
merge(vv,vv2,all=TRUE)
--
David Winsemius
More information about the R-help
mailing list