[R] How to merge 3 data frames by rownames?

Eric Berger er|cjberger @end|ng |rom gm@||@com
Tue Nov 5 17:50:09 CET 2019


I think your code is a bit buggy. Try this

for(i in 2:length(l)) {
  allF <- merge(allF, l[[i]],  by= "row.names", all.x= F, all.y= F)
  rownames(allF) <- allF$Row.names
  allF <- allF[,-1]
}

HTH,
Eric


On Tue, Nov 5, 2019 at 6:16 PM Ana Marija <sokovic.anamarija using gmail.com>
wrote:

> Hi,
>
> I have 3 data frames like this:
>
> > head(s11)
>                       B_NoD
> Ebfrl.7uOZfnjp_E7k 7.583709
> ueQUrXd5FH554RlhZc 5.177791
> 0Uu3XrB6Bd14qoNeuc 4.680306
> 0t7nhVLii6tSAxtLhc 4.565023
> fSUyR.vR7Xu0iR4nUU 2.885992
> 0Tm7hdRJxd9zoevPlA 2.866847
> > head(s22)
>                      B_DwoC
> Ebfrl.7uOZfnjp_E7k 7.583709
> ueQUrXd5FH554RlhZc 5.177791
> 0Uu3XrB6Bd14qoNeuc 4.680306
> 0t7nhVLii6tSAxtLhc 4.565023
> fSUyR.vR7Xu0iR4nUU 2.885992
> 0Tm7hdRJxd9zoevPlA 2.866847
> > head(s33)
>                       B_DwC
> Ebfrl.7uOZfnjp_E7k 7.583709
> ueQUrXd5FH554RlhZc 5.177791
> 0Uu3XrB6Bd14qoNeuc 4.680306
> 0t7nhVLii6tSAxtLhc 4.565023
> fSUyR.vR7Xu0iR4nUU 2.885992
> 0Tm7hdRJxd9zoevPlA 2.866847
>
> I tried merging them using:
>
> rn <- rownames(s11)
> l <- list(s11, s22, s33)
> allF <- l[[1]]
> for(i in 2:length(l)) {
>   dat <- merge(allF, l[[i]],  by= "row.names", all.x= F, all.y= F) [,-1]
>   rownames(allF) <- rn
> }
>
> but my allF has only one column in results, while it would have all 3
> from all 3 data frames:
>
> > head(allF)
>                       B_NoD
> Ebfrl.7uOZfnjp_E7k 7.583709
> ueQUrXd5FH554RlhZc 5.177791
> 0Uu3XrB6Bd14qoNeuc 4.680306
> 0t7nhVLii6tSAxtLhc 4.565023
> fSUyR.vR7Xu0iR4nUU 2.885992
> 0Tm7hdRJxd9zoevPlA 2.866847
>
> Please advise,
> Ana
>
> ______________________________________________
> R-help using 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]]



More information about the R-help mailing list