使用R中的html表创建滚动日和月表

时间:2018-02-20 03:27:53

标签: r dataframe matrix html-table dplyr

我有一个如下所示的数据框:

ID     Date1                     T1     Date2     Val1
A-1    2018-01-10 15:05:24       A    2018-01-15  10
A-2    2018-01-05 14:15:22       B    2018-01-14  12
A-3    2018-01-04 13:20:21       A    2018-01-13  15
A-4    2018-01-01 18:35:45       B    2018-01-12  22
A-5    2017-12-28 19:45:10       A    2018-01-11  18
A-6    2017-12-10 08:03:29       A    2018-01-10  21
A-7    2017-12-06 20:55:55       A    2018-01-09  28
A-8    2018-01-10 10:02:12       A    2018-01-15  10
A-9    2018-01-05 17:15:14       B    2018-01-14  12
A-10   2018-01-04 18:35:58       A    2018-01-13  15
A-11   2018-01-01 21:09:25       B    2018-01-12  22
A-12   2017-12-28 02:12:22       A    2018-01-11  18
A-13   2017-12-10 03:45:44       A    2018-01-10  21
A-14   2017-12-06 07:15:25       A    2018-01-09  28

从上面的数据框中我想创建一个下面提到的小数据框并将其转换为htmltable格式,可以使用mailR库轻松发送电子邮件。

enter image description here

Conditions:
1. Consider `Date2` for the `# of A` and `# of B` For both Date and month report.
2. `# of A` mean count of where `T1` is A for the same date and month. (same for B)
3. `Sum of A` mean sum of `Val1` for the same date and month. (same for B).
4. `Average of A` means Average of where `T1` is A for the same date and month. (same for B)
5. `Avg Time A` means Average of `Date2`-`Date1` value for "A" for the same date and month. (same for B)
6. I want these date for the last 7 days rolling back based on date available in Dataframe. (In dataframe data should be of 365 days but i want image for only last seven days rolling back)
7. For `A & B Consolidated` # of A and sum of B should be as per same logic but for month considering `Date2`.
8. For `MOM Growth` the Formula would be (i.e =(Feb-18-Jan-18)/Jan-18 in % (-) if negative)
9. `A & B Consolidated` should also be in 7 month rolling and it should automatically change the month if 8th month comes from first day.

1 个答案:

答案 0 :(得分:2)

以下尝试使用dplyrlubridatetableHTML

library(dplyr)
library(lubridate)
library(tableHTML)

我使用名为data.frame的{​​{1}},如下所示:

my_data

然后我将日期字段更改为my_data <- read.table(text = "ID Date1 T1 Date2 Val1 A-1 '2018-01-10 15:05:24' A 2018-01-15 10 A-2 '2018-01-05 14:15:22' B 2018-01-14 12 A-3 '2018-01-04 13:20:21' A 2018-01-13 15 A-4 '2018-01-01 18:35:45' B 2018-01-12 22 A-5 '2017-12-28 19:45:10' A 2018-01-11 18 A-6 '2017-12-10 08:03:29' A 2018-01-10 21 A-7 '2017-12-06 20:55:55' A 2018-01-09 28 A-8 '2018-01-10 10:02:12' A 2018-01-15 10 A-9 '2018-01-05 17:15:14' B 2018-01-14 12 A-10 '2018-01-04 18:35:58' A 2018-01-13 15 A-11 '2018-01-01 21:09:25' B 2018-01-12 22 A-12 '2017-12-28 02:12:22' A 2018-01-11 18 A-13 '2017-12-10 03:45:44' A 2018-01-10 21 A-14 '2017-12-06 07:15:25' A 2018-01-09 28", header = TRUE, stringsAsFactors = FALSE) POSIXct,按Date分组,并按照描述汇总数据。这适用于Date2A。然后合并2个结果结构,用空字符串替换B s。

NA

然后我使用table_1 <- merge( my_data %>% mutate(Date1 = lubridate::ymd_hms(Date1), Date2 = lubridate::ymd(Date2)) %>% filter(T1 == "A") %>% group_by(Date2) %>% summarise("# of A" = n(), "sum of A" = sum(Val1), "Average of A" = mean(Val1), "Avg Time A" = round(mean(difftime(Date2, Date1)), 2)), my_data %>% mutate(Date1 = lubridate::ymd_hms(Date1), Date2 = lubridate::ymd(Date2)) %>% filter(T1 == "B") %>% group_by(Date2) %>% summarise("# of B" = n(), "sum of B" = sum(Val1), "Average of B" = mean(Val1), "Avg Time B" = round(mean(difftime(Date2, Date1)), 2)) , by = "Date2", all = TRUE) table_1[is.na(table_1)] <- "" 创建一个HTML表格:

tableHTML

看起来像这样: table_1

接下来,我使用相同的逻辑和一些柚木:

  • 数据按table_1 %>% tableHTML(rownames = FALSE, widths = rep(100, 9), second_headers = list(c(1, 4, 4), c("", "Status of A", "Status of B")))
  • 排序
  • 仅使用Date2# of A|B
  • sum of A|B用于获取MOM更改

lag()

再次使用table_2 <- merge( my_data %>% mutate(Date2 = ymd(Date2)) %>% arrange(Date2) %>% mutate(Month = paste(month(ymd_hms(Date1), label = TRUE), year(Date1), sep = "-")) %>% filter(T1 == "A") %>% group_by(Month) %>% summarise("# of A" = n(), "sum of A" = sum(Val1)) %>% mutate("MOM Growth # of A" = round(apply(cbind(`# of A`, lag(- `# of A`)), 1, sum, na.rm = TRUE) / lag(`# of A`), 2), "MOM Growth sum of A" = round(apply(cbind(`sum of A`, lag(- `sum of A`)), 1, sum, na.rm = TRUE) / lag(`sum of A`) * 100, 2)) %>% select(Month, `# of A`, `MOM Growth # of A`, `sum of A`, `MOM Growth sum of A`), my_data %>% mutate(Date2 = ymd(Date2)) %>% arrange(Date2) %>% mutate(Month = paste(month(ymd_hms(Date1), label = TRUE), year(Date1), sep = "-")) %>% filter(T1 == "B") %>% group_by(Month) %>% summarise("# of B" = n(), "sum of B" = sum(Val1)) %>% mutate("MOM Growth # of B" = round(apply(cbind(`# of B`, lag(- `# of B`)), 1, sum, na.rm = TRUE) / lag(`# of B` * 100), 2), "MOM Growth sum of B" = round(apply(cbind(`sum of B`, lag(- `sum of B`)), 1, sum, na.rm = TRUE) / lag(`sum of B`) * 100), 2) %>% select(Month, `# of B`, `MOM Growth # of B`, `sum of B`, `MOM Growth sum of B`), by = "Month", all = TRUE) table_2[is.na(table_2)] <- ""

tableHTML

enter image description here