vendredi 23 août 2019

R: Perform multiple if conditions across two data frames of different length using for loop

I am fairly new to R and have to solve a (for me) quite complex problem -> I hope for your help!

I have 2 dataframes of different length:

Product <- c("A1", "A2", "C1", "D1")
Posting_Date <- c("01-2016", "03-2016", "02-2016", "01-2016")

df1 <- data.frame(Product, Posting_Date)

df1
 Product Posting_Date
1      A1      01-2016
2      A2      03-2016
3      C1      02-2016
4      D1      01-2016

Product2 <- rep(c("A1", "A2", "B1", "C1", "C2", "D1"), each = 3)
Sales_Month <- rep(c("01-2016", "02-2016", "03-2016"), times = 6)
Sales <- rep(c(2300,0,2700,250,0,3700), times =3)
df2 <- data.frame(Product2, Sales_Month, Sales)

df2
 Product2 Sales_Month Sales
1        A1     01-2016  2300
2        A1     02-2016     0
3        A1     03-2016  2700
4        A2     01-2016   250
5        A2     02-2016     0
6        A2     03-2016  3700
7        B1     01-2016  2300
8        B1     02-2016     0
9        B1     03-2016  2700
10       C1     01-2016   250
11       C1     02-2016     0
12       C1     03-2016  3700
13       C2     01-2016  2300
14       C2     02-2016     0
15       C2     03-2016  2700
16       D1     01-2016   250
17       D1     02-2016     0
18       D1     03-2016  3700

I want to add an additional column to df1 that displays a value of 1 if for every product listed in df1 there are sales > 0 in the corresponding month in df2 (df1$Posting_Month == df2$Sales_Month) and a value of 0 if sales were 0 or <0.

This is what I have tried:

for(i in 1:dim(df1) [1]) {
  if (df1$Product == df2$Product2)
    &
    if (df1$Posting_Date == df2$Sales_Month)
      &
    if (df2$Sales > 0) {
      df1$match <- 1
    } else {df1$match <- 0}
}

But it gives me several error messages:

Error: unexpected '&' in:
"  if (df1$Product == df2$Product2)
    &"
Error: unexpected '&' in:
"    if (df1$Posting_Date == df2$Sales_Month)
      &"
>     if (df2$Sales > 0) {
+       df1$match <- 1
+     } else {df1$match <- 0}
Warning message:
In if (df2$Sales > 0) { :
  the condition has length > 1 and only the first element will be used
> }
Error: unexpected '}' in "}"

The correct output should look something like this:

 Product Posting_Date match
1      A1      01-2016     1
2      A2      03-2016     1
3      C1      02-2016     0
4      D1      01-2016     1

Can somebody please help me to solve this problem?

Aucun commentaire:

Enregistrer un commentaire