0

I have 3 questions relating to the compare_df() function within the compareDF CRAN package.

I have two data frames with identical structures but different contents (this_week and last_week):

this_week
  Week   A   B   C
1    1   0   0   0
2    2   0   1   0
3    3   0   1   0
4    4   2   1   0
5    5   2   0   0       

last_week
  Week   A   B   C
1    1   0   0   0
2    2   0   0   0
3    3   0   0   1
4    4   3   0   0
5    5   0   0   0

I am using compare_df(this_week, last_week, group_col = "Week") to compare these two data frames. Specifically, I am interested in the second of the compare_df() function outputs which gives cell-level comparisons.

The output shows which cells have increased from one week to the next:

weeks_compared <- compare_df(this_week, last_week, group_col = "Week")
weeks_compared

$comparison_df
  Week chng_type   A   B   C
1    2         +   0   1   0
2    2         -   0   0   0
3    3         +   0   1   0
4    3         -   0   0   1
5    4         +   2   1   0
6    4         -   3   0   0
7    5         +   2   0   0
8    5         -   0   0   0

$comparison_table_diff
  Week chng_type   A   B   C
1    =         +   =   +   =
2    =         -   =   -   =
3    =         +   =   +   +
4    =         -   =   -   -
5    =         +   +   +   =
6    =         -   -   -   =
7    =         +   +   =   =
8    =         -   -   =   =

Interestingly, row 5 and 6 do not provide the comparison results that I would expect. I would expect:
row 5, column 3 ("A") of the second dataframe ($comparison_table_diff) to be "-"
row 6, column 3 ("A") to be "+".
However, it is actually the opposite way around:

$comparison_df
  Week chng_type A B C
5    4         + 2 1 0
6    4         - 3 0 0

$comparison_table_diff
  Week chng_type A B C
5    =         + + + =
6    =         - - - =

1) Does anyone know why this happens?

In addition, I do not know how to use this output further. My aims are:
2) To update the old data which has increased in last_week
3) To add an asterisk to the last_week data which has increased (in columns "B" and "C" only)

I have not found anything related to actually using the compare_df() outputs on Stack Overflow other than to simply paste these tables, which isn't sufficient for my task.

I wondered if anyone has done anything similar and/or could share some ideas of how I might go about reaching these two aims. Alternatively, would be interested to know if there is a better package to use/workaround for this task. And of course, let me know if there is any further information that's required.

Thanks in advance for any help you can provide!

  • Hi Thomas, welcome to StackOverflow, and an interesting, well constructed question. You might consider `dput`ing your `this_week` and `last_week` for those of us cut/pasters as it will speed things. To your expected output part, are row 5 and 6 correct if you swap them to (last_week, this_week) seemingly reversing the time series? When you `str(weeks_compared)` what classes are indicated as that might guide how you'll further process outputs. – Chris Apr 22 '21 at 17:27
  • Hi Chris, thank you for the warm welcome and for the prompt response and feedback! I will definitely try to `dput` where I can in future (thanks for understanding). When I swap row 5 and 6 then it gives the correct expected output (row 5, column 3 ("A") is now "+ve" (from `this_week`), whereas row 6, column 3 ("A") is now "-ve" (from `last_week`)). When I run `str(weeks_compared)`, the classes of all of the variables within `last_week` and `this_week` are all `num`. The comparison outputs in `$comparison_table_diff` ("=", "+", "-") are `chr`, as is the `chng_type` variable in `$comparison_df`. – Thomas A. Anderson Apr 22 '21 at 17:58
  • I was suggesting swapping the whole `this_week, last_week`, for `last_week, this_week`, rather than just the rows (just as an easy way to get a sense of it against your expectations). I don't really understand wanting to amend prior data. Anyway,imagining not amending, there's the interesting problem of taking char c('-', '+','=') to minus, plus,equal as math Ops (char to builtins). Head scratch. – Chris Apr 23 '21 at 01:12

0 Answers0