[R] merging single column from different dataframe
Rui Barradas
ruipbarradas at sapo.pt
Sun Jun 3 22:06:37 CEST 2012
Hello,
One way, with limited applicability, would be
df4 <- cbind(df2, var1=NA)
df4$var1[ df2$names2 %in% df1$names1 ] <- df1$var1
df4
Limited because it doesn't resist to duplicate key values (the columns
names1/2).
But with unique keys it should be much more efficient, speed- and
memory-wise. Much, much more.
There is also package data.table, designed for this type of problem.
As for the extraction operator, there is An Introduction to R,
R-intro.pdf, in the doc directory of your R installation.
And several others on-line, on CRAN, for instance.
(Your example wouldn't work, df2$names2 are the values of that variable,
not row indexes. See df2[[ "names2" ]].)
Hope this helps,
Rui Barradas
Em 03-06-2012 20:22, Kai Mx escreveu:
> Hi all,
> probably really simple to solve, but having no background in programming I
> haven't been able to figure this out: I have two dataframes like
>
> df1<- data.frame(names1=c('aa','ab', 'ac', 'ad'), var1=c(1,5,7,12))
> df2<- data.frame(names2=c('aa', 'ab', 'ac', 'ad', 'ae'),
> var2=c(3,6,9,12,15))
>
> Now I want merge var1 to df2 by matching the dataframes by the 'names'
> columns, i.e. something like
>
> df3<- merge (df2, df1, by.x='names2', by.y='names1', all.x=T)
>
> However, the original dataframes have quite a lot of columns and I thought
> that I should be able to address the var1 column by something like
> df1$var[[df2$name2]]. Could somebody please enlighten me and/or maybe
> suggest a short tutorial for the extraction operator?
> Thanks!
>
>
> Best,
>
> Kai
>
> [[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.
More information about the R-help
mailing list