Display 2 Numbers in Dataframe cell

Viewed 88

I have to Dataframes with the same column and row names. i would like to merge them but display both original values seperated by "/" or "," (basically any kind of seperation). I do not want simplified fractions, i need the original numbers. as an example, for this 2 input df´s:

df1 <-
         A    B
 a       1    2
 b       3    3
 c       4    2
 d       2    1

df2 <-
             A    B
     a       0    1
     b       2    2
     c       4    2
     d       1    0

i would like this output

df3 <-
         A    B
 a       0/1  1/2
 b       2/3  2/3
 c       4/4  2/2
 d       1/2  0/1

edit: the rownames are not in the df

1 Answers

You can use mapply with paste and add the rownames.

df3 <- mapply(paste, df2, df1, sep="/")
rownames(df3) <- rownames(df1)
df3
#  A     B    
#a "0/1" "1/2"
#b "2/3" "2/3"
#c "4/4" "2/2"
#d "1/2" "0/1"
Related