I have two data sets. The aim is to add a row to df1 if a new code appears in the df2 and the df2$LABEL is set to PRIV. I tried simply with the if clause, but it didn't work for me. I think maybe there is a solution with dplyr library. Logics I used (maybe will be helpful):
df1$CODE != df2$CODE & df2$LABEL == "PRIV"
Dummy examples of datasets with dput (df1 first and df2 second):
structure(list(OUTLETID = c("11N", "12B", "17C",
"44O"), CODE = c(4623,
4623, 4111, 4343),
PRICE = c(1.45, 1.45, 5.45, 5.03), LABEL = c("PRIV", "PRIV", "NON PRIV", "NON PRIV")),
row.names = c(NA, -4L), class = c("tbl_df", "tbl", "data.frame"))
structure(list(CODE = c(4623, 4205, 4551),
PRICE = c(1.45, 1.17, 5.99), LABEL = c("PRIV", "PRIV", "NON PRIV")),
row.names = c(NA, -3L), class = c("tbl_df", "tbl", "data.frame"))
So in this example, a new row with CODE=4205, PRICE=1.17 and LABEL="PRIV" should be added to df1. As in df2 there is no OUTLETID, should be just NA in that column. This code should work for the general case, bigger datasets, not just for this small example.
Aucun commentaire:
Enregistrer un commentaire