Automate column subtraction in R

Asked

Viewed 108 times

1

I have a table in the R of people, countries and days. I need to subtract the amount of people from the previous day from the current day column for all columns, someone can help me?

I need to make the following formula in an automated way

05/11/20 = 05/11/20 - 04/11/20 para todas as linhas

04/11/20 = 04/11/20 - 03/11/20 para todas as linhas

03/11/20 = 03/11/20 - 02/11/20 para todas as linhas

02/11/20 = 02/11/20 - 01/11/20 para todas as linhas

I tried something like this, but it doesn’t work

mutate([c(4)] = [c(4)] - [c(4 - 1)]))

After this formula would put inside a loop to change the columns

In case the first table always shows the total number of people, need to know the amount day by day.

My base is like this:

Países  01/11/2020  02/11/2020  03/11/2020  04/11/2020  05/11/2020  06/11/2020

Brasil  1           1           2           3           5           7

Chile   2           2           2           4           4           5

US      0           0           0           1           2           3

México  0           0           0           0           1           1

I need her like this:

Países  01/11/2020  02/11/2020  03/11/2020  04/11/2020  05/11/2020  06/11/2020

Brasil  1           0           1           1           2           2

Chile   2           0           0           2           0           1

US      0           0           0           1           1           1

México  0           0           0           0           1           0

2 answers

5

You can subtract the data frame by "moving" the "day/column" by making a Slice.

Here we have the data frame (first slice)

df[3:ncol(df)]

  `02/11/2020` `03/11/2020` `04/11/2020` `05/11/2020` `06/11/2020`
1            1            2            3            5            7
2            2            2            4            4            5
3            0            0            1            2            3
4            0            0            0            1            1

And here we have (second slice):

df[3:ncol(df) - 1]

  `01/11/2020` `02/11/2020` `03/11/2020` `04/11/2020` `05/11/2020`
1            1            1            2            3            5
2            2            2            2            4            4
3            0            0            0            1            2
4            0            0            0            0            1

Note that the two are the same size.


Full exit:

cbind.data.frame(df[1:2], df[3: ncol(df)] - df[3:ncol(df) - 1])

Países 01/11/2020 02/11/2020 03/11/2020 04/11/2020 05/11/2020 06/11/2020
Brasil          1          0          1          1          2          2
 Chile          2          0          0          2          0          1
    US          0          0          0          1          1          1
México          0          0          0          0          1          0

4


The function diff calculates difference between elements. Function apply applies a function to a dimension. In case it applies to the lines of the data frame., it will return a transposed matrix, which can be reversed with t:

> t(apply(dados[-1], 1, diff))
    02/11/2020 03/11/2020 04/11/2020 05/11/2020 06/11/2020
[1,]          0          1          1          2          2
[2,]          0          0          2          0          1
[3,]          0          0          1          1          1
[4,]          0          0          0          1          0

The [-1] is not to use the first column. To get back a date.frame with the names:

cbind(dados[1], t(apply(dados[-1], 1, diff)))

Note that the column "01/11/2020" was cut from the result because it has no difference calculation for it. You can use cbind(dados[1:2], ... to include it, but it is not recommended.

Data used

dados <- read.table(text = c('
  "Países" "01/11/2020" "02/11/2020" "03/11/2020" "04/11/2020" "05/11/2020" "06/11/2020"
  Brasil  1           1           2           3           5           7
  Chile   2           2           2           4           4           5
  US      0           0           0           1           2           3
  México  0           0           0           0           1           1'),
  header = TRUE, check.names = FALSE)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.