This is a test version of Biostars. For the public version, visit https://www.biostars.org.
merge two data frame with different length

Hello everyone,

I have two data frames which don't have the same length and one has more values than the other I would like to merge them and add an "NA" where there are missing values

Does anyone knows how to do that?

head(healthy_cell)
rep_names    TE_classes
1      HAL1          LINE
2      HAL1          LINE
3      HAL1          LINE
4      L1M4          LINE
5  (CACCC)n Simple_repeat
6      L1M4          LINE

x <-healthy_cell$rep_names
healthy_count= tapply(x,x,length)
df1 <- data.frame(healthy_count)

                healthy_count
(A)n                 92
(AATAG)n         1
(AATTG)n          1
(AGGGGG)n      5
(AGGTG)n          5
(AGTAG)n          1

# same for the second file(cancer)

y <- cancer_cell$rep_names
cancer_count=tapply(y,y,length)
df2 <- data.frame(cancer_count)
               cancer_count
(A)n                   89
(AAATG)n            1
(AATAG)n            1
(ACCG)n              1
(ACTG)n               2
(AGCTG)n             2

When I tried to merge the two data frames it gave me

merge(df1,df2,by=intersect(names(df1),names(df2)),all.df1=by,all.df2=by,all=T)
healthy_count cancer_count
1            92           89
2             1           89
3             1           89
4             5           89
5             5           89
6             1           89

However I would like to have all the rep_names as well

(AAATG)n    (ACTG)n   (AGCTG)n ,....
r

Thanks everyone for all the ideas it worked!!!!!!

3 answers

What about

merge(df1, df2, by = "row.names", all = TRUE)

That will add a new column to the resulting data frame containing the row names of the original two data frames

Have you tried full_join() from dplyr?

cbind the rownames and then merge by the names.

df1 = cbind("id"=rownames(df1),df1)
df2 = cbind("id"=rownames(df2),df2)
merge(df1,df2,by="id",all=T)

Log in to answer this question.