This is a problem of merging two data frames such that all the records are included. The approach used is Tidyverse I have two data frames, df1 and df2 that look as follows. > df1 <- data.frame( A = c(1, 2, 3, 4, 5, 6, 7, 8, 9), B = c("a", "b", "c", "d", "e", "f", "g", "h", "i")) > df2 <- data.frame( A = c(1, 2, 11), C = c("as", "ba", "js")) I want to merge the two data frames into a new one, and make sure that in column A, all cases (i.e, 1 to 11) are included. I want the contents of both B and C to be included too. This is the code that ultilize *full_join()* to merge the dataframes, with pipe operation. > result <- df1 %>% #Put in df1 full_join(df2, by = "A") %>% #Join it with df2,and by what is in A complete(A = 1:11) %>% # complete() adds rows for missing values in A replace_na(list(B = "N/A", C = "N/A")) # finally replace NAs with "N/A" > print(result) # Display results  
author | snippets |
---|---|
permlink | r-merging-two-data-frames-such-that-all-the-records-are-included |
category | hive-138200 |
json_metadata | {"app":"peakd/2023.9.2","format":"markdown","tags":["coding","programming","development","proofofbrain","stemgeeks","datascience","rstats","r"],"users":[],"image":["https://files.peakd.com/file/peakd-hive/snippets/AKNMuVqPrNWuLjdyxwLdzm99obv1dcnZWufeJdBoWwecd9UBvGxERepophy4Epu.png","https://files.peakd.com/file/peakd-hive/snippets/23tSzLNbpN54RvCEy8HyEfmWuv1eDABAseRmCPUkYzra8ooTAwAhA6wLUdSXjUaHzAZm3.png"]} |
created | 2023-10-10 02:09:51 |
last_update | 2023-10-10 02:09:51 |
depth | 0 |
children | 0 |
last_payout | 2023-10-17 02:09:51 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.064 HBD |
curator_payout_value | 0.063 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 1,219 |
author_reputation | 801,725,525,485 |
root_title | "R: Merging two data frames such that all the records are included" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 0 |
post_id | 127,854,822 |
net_rshares | 316,216,521,337 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
bluesniper | 0 | 34,022,241,929 | 100% | ||
holovision.stem | 0 | 910,466,702 | 100% | ||
irivers | 0 | 144,571,604 | 100% | ||
cryptothesis | 0 | 278,136,904,154 | 100% | ||
hive-117638 | 0 | 1,747,661,343 | 50% | ||
taimen | 0 | 1,254,675,605 | 60% | ||
meykse16 | 0 | 0 | 20% |