0

I have the following two DFs, each with two columns (stringIDs, and counts). Data looks like:

I'd like to transform this to 1 DF, sorted A to Z, (with all stringIDs from both DFs, counts from DF1, counts from DF2). If the stringID does not exist, the corresponding count should be 0. Is there a package in R that will allow me to do this transformation?

I have: enter image description here

I'd like the data transformed to: enter image description here

April
  • 89
  • 7
  • Also for next questions please add data in reproducible format ! You can use `dput(df)` for that. – Duck Jul 30 '20 at 21:29

1 Answers1

0

Try this. It is a merge task:

#Data
df1 <- data.frame(stringid=paste0('string',1:4),counts=c(10,11,11,13),stringsAsFactors = F)
df2 <- data.frame(stringid=paste0('string',c(1,3:5)),counts=c(10,11,11,10),stringsAsFactors = F)

#Merge
dfmerged <- merge(df1,df2,by='stringid',all=T,suffixes = c('_df1','_df2'))
dfmerged[is.na(dfmerged)]<-0

  stringid counts_df1 counts_df2
1  string1         10         10
2  string2         11          0
3  string3         11         11
4  string4         13         11
5  string5          0         10
Duck
  • 39,058
  • 13
  • 42
  • 84