如何旋转R中带有节和子节的列组成的数据框


12

我有一个下面提到的数据框:

structure(
  list(ID = c("P-1", " P-1", "P-1", "P-2", "P-3", "P-4", "P-5", "P-6", "P-7",
              "P-8"),
       Date = c("2020-03-16 12:11:33", "2020-03-16 13:16:04",
                "2020-03-16 06:13:55", "2020-03-16 10:03:43",
                "2020-03-16 12:37:09", "2020-03-16 06:40:24",
                "2020-03-16 09:46:45", "2020-03-16 12:07:44",
                "2020-03-16 14:09:51", "2020-03-16 09:19:23"),
       Status = c("SA", "SA", "SA", "RE", "RE", "RE", "RE", "XA", "XA", "XA"),
       Flag = c("L", "L", "L", NA, "K", "J", NA, NA, "H", "G"),
       Value = c(5929.81, 5929.81, 5929.81, NA, 6969.33, 740.08, NA, NA, 1524.8,
                 NA),
       Flag2 = c("CL", "CL", "CL", NA, "RY", "", NA, NA, "", NA),
       Flag3 = c(NA, NA, NA, NA, "RI", "PO", NA, "SS", "DDP", NA)),
  .Names=c("ID", "Date", "Status", "Flag", "Value", "Flag2", "Flag3"),
  row.names=c(NA, 10L), class="data.frame")

我正在使用以下代码:

    df %>% mutate(L = ifelse(Flag == "L",1,0),
                  K = ifelse(Flag == "K",1,0),
                  # etc for Flag) %>%
      mutate(sub_status = NA) %>%
      mutate(sub_status = ifelse(!is.na(Flag2) & Flag3 == 0, "a", sub_status),
             sub_status = ifelse(is.na(Flag2) & Flag3 != 0, "b", sub_status),
             # etc for sub-status) %>%
      mutate(value_class = ifelse(0 <= Value & Value <= 15000, "0-15000",
                                  "15000-50000")) %>%
      group_by(Date, status, sub_status, value_class) %>%
      summarise(L = sum(L),
                K = sum(K),
                # etc
                count = n())

为我提供以下输出:

    Date         Status  sub_status   value_class G H I J K L NA Count
    2020-03-20   SA      a            0-15000     0 0 0 0 1 1 0  2
    2020-03-20   SA      b            0-15000     0 0 0 0 1 0 0  1
    ................
    ................

我想使用来获得以下输出DF,其中该Status列具有不同的3个值,并且Flag2具有value或[null]或NA,最后该Flag3列具有不同的7个值,具有[null]或NA。对于一个不同IDFlag3列,我们有多个列。

我需要通过创建一个基于Value0-15000、15000-50000 的3组来创建以下数据框。

  • 如果对于唯一ID,Flag2其值不是0或[null] / NA,但Flag3值是0或[null] / NA,则它将是a
  • 如果对于唯一ID,Flag3其值不是0或[null] / NA,但Flag2值是0或[null] / NA,则它将是b
  • 如果对于唯一ID,两个Flag2Flag3都具有非0或[Null] / NA的值,则它将为c
  • 如果对于唯一的ID两者,Flag2Flag3都为0或[Null] / NA,则为d

我想将上述datafrmae安排在带有percentand Total列的以下结构中。

我已经提到过2/5要显示的百分比,即状态将除以总计,而状态将sub_status除以其各自的百分比Status

16/03/2020         0 - 15000                    15000 - 50000
Status  count   percent  L K J H G [Null]    count   percent  L K J H G [Null]   Total
SA        1 1/8 (12.50%) 1 0 0 0 0   0         0       -      0 0 0 0 0    0       1
a         1 1/1(100.00%) 1 0 0 0 0   0         0       -      0 0 0 0 0    0       1
b         0       -      0 0 0 0 0   0         0       -      0 0 0 0 0    0       0
c         0       -      1 0 0 0 0   0         0       -      0 0 0 0 0    0       0
d         0       -      0 0 0 0 0   0         0       -      0 0 0 0 0    0       0
RE        4      50.00%  0 1 1 0 0   2         0       -      0 0 0 0 0    0       4
a         0        -     0 0 0 0 0   0         0       -      0 0 0 0 0    0       0
b         1      25.00%  0 0 1 0 0   1         0       -      0 0 0 0 0    0       1
c         1      25.00%  0 1 0 0 0   1         0       -      0 0 0 0 0    0       1
d         2      50.00%  0 0 0 0 0   2         0       -      0 0 0 0 0    0       2
XA        3      37.50%  0 0 0 1 1   1         0       -      0 0 0 0 0    0       3
a         0        -     0 0 0 0 0   0         0       -      0 0 0 0 0    0       0
b         2      66.67%  0 0 0 1 0   1         0       -      0 0 0 0 0    0       2
c         0        -     0 0 0 0 0   0         0       -      0 0 0 0 0    0       0
d         1      33.33%  0 0 0 0 1   0         0       -      0 0 0 0 0    0       1
Total     8     100.00%  1 1 0 0 1   3         0       -      0 0 0 0 0    0       8

我已经提到了基于最新日期为16/03/2020的必需输出,如果该数据框没有最新日期,则将startdate所有值0保留在输出数据框中。百分比列仅供参考,将计算出百分比值。

另外,我想保持结构静态。例如,如果一天中不存在任何参数,则输出结构将与0值相同。

例如,假设date 17/03/2020没有任何带有status SA或sub_status c的行,则占位符将出现在输出中,值为0


@akrun:我保留的百分比列2/5仅用于表示目的。只有百分比值,带小数点的2个小数点带有百分比符号。
user9211845

@akrun:请建议是否可以通过R :(
user9211845

您的数据输入为10行,但预计还会更多。是基于输入示例的预期
akrun

@akrun:对不起,但是输出仅用于视觉表示。我需要了解获取此类输出的方法。
user9211845

1
您可以从所需dput的数据集开始-这是第三个代码块。由于您似乎对输出内容满意,因此前面的代码似乎不相关。
科尔

Answers:


3

希望这足以使您入门,继续下去,我需要一个预期的输出,看起来像是来自R的输出,以及有关如何计算变量的进一步说明。

library(tidyverse)
df <- structure(
  list(ID = c("P-1", " P-1", "P-1", "P-2", "P-3", "P-4", "P-5", "P-6", "P-7",
              "P-8"),
       Date = c("2020-03-16 12:11:33", "2020-03-16 13:16:04",
                "2020-03-16 06:13:55", "2020-03-16 10:03:43",
                "2020-03-16 12:37:09", "2020-03-16 06:40:24",
                "2020-03-16 09:46:45", "2020-03-16 12:07:44",
                "2020-03-16 14:09:51", "2020-03-16 09:19:23"),
       Status = c("SA", "SA", "SA", "RE", "RE", "RE", "RE", "XA", "XA", "XA"),
       Flag = c("L", "L", "L", NA, "K", "J", NA, NA, "H", "G"),
       Value = c(5929.81, 5929.81, 5929.81, NA, 6969.33, 740.08, NA, NA, 1524.8,
                 NA),
       Flag2 = c("CL", "CL", "CL", NA, "RY", "", NA, NA, "", NA),
       Flag3 = c(NA, NA, NA, NA, "RI", "PO", NA, "SS", "DDP", NA)),
  .Names=c("ID", "Date", "Status", "Flag", "Value", "Flag2", "Flag3"),
  row.names=c(NA, 10L), class="data.frame")

df2 <- df %>%
  mutate(
    # add variables
    Value = ifelse(0 <= Value & Value <= 15000, "0-15000", "15000-50000"),
    substatus = case_when(
      !is.na(Flag2) & is.na(Flag3) ~ "a",
      !is.na(Flag3) & is.na(Flag2) ~ "b",
      !is.na(Flag3) & !is.na(Flag2) ~ "c",
      TRUE ~ "d"),
    # make Date an actual date rather than a timestamp
    Date = as.Date(Date),
    # remove obsolete columns
    Flag2 = NULL,
    Flag3 = NULL,
    ID = NULL,
    # renames NAs into the name of the desired column
    Flag = ifelse(is.na(Flag), "[Null]", Flag),
    # create column of 1 for pivot
    temp = 1,
    # and row id
    id = row_number()
    ) %>%
  # create new columns L K etc, this also drops the Flag col
  pivot_wider(names_from = "Flag", values_from = "temp", values_fill = list(temp=0)) %>%
  # move `[Null]` column to the end
  select(everything(), -`[Null]`, `[Null]`) %>%
  mutate(
    id = NULL,
    count = 1,
    Total = rowSums(select(., L:`[Null]`))) 
df2
#> # A tibble: 10 x 12
#>    Date       Status Value substatus     L     K     J     H     G `[Null]`
#>    <date>     <chr>  <chr> <chr>     <dbl> <dbl> <dbl> <dbl> <dbl>    <dbl>
#>  1 2020-03-16 SA     0-15~ a             1     0     0     0     0        0
#>  2 2020-03-16 SA     0-15~ a             1     0     0     0     0        0
#>  3 2020-03-16 SA     0-15~ a             1     0     0     0     0        0
#>  4 2020-03-16 RE     <NA>  d             0     0     0     0     0        1
#>  5 2020-03-16 RE     0-15~ c             0     1     0     0     0        0
#>  6 2020-03-16 RE     0-15~ c             0     0     1     0     0        0
#>  7 2020-03-16 RE     <NA>  d             0     0     0     0     0        1
#>  8 2020-03-16 XA     <NA>  b             0     0     0     0     0        1
#>  9 2020-03-16 XA     0-15~ c             0     0     0     1     0        0
#> 10 2020-03-16 XA     <NA>  d             0     0     0     0     1        0
#> # ... with 2 more variables: count <dbl>, Total <dbl>

# As you didn't tell what to do with NA values so I left them as NA 

bind_rows(
  df2 %>%
    # add missing combinations of abcd
    complete(nesting(Date, Status, Value), substatus) %>%
    group_by(Date, Value, Status, substatus) %>% 
    summarize_all(~sum(., na.rm=TRUE)) %>%
    group_by(Status, Value) %>%
    mutate(percent = paste(round(100 * Total / sum(Total), 2), "%")) %>%
    ungroup(),
  df2 %>% 
    mutate(substatus = Status, Status = paste0(Status, "_")) %>%
    group_by(Date, Value, Status, substatus) %>% 
    mutate(count = n()) %>%
    group_by(count, add = TRUE) %>%
    summarize_all(~sum(., na.rm=TRUE)) %>%
    group_by(Value) %>%
    mutate(percent = paste(round(100 * Total / sum(Total), 2), "%"))
) %>%
  arrange(Date, Value, desc(Status)) %>%
  mutate(Status = NULL) %>%
  rename(Status = substatus) %>%
  print(n=Inf)
#> # A tibble: 25 x 12
#>    Date       Value Status     L     K     J     H     G `[Null]` count Total
#>    <date>     <chr> <chr>  <dbl> <dbl> <dbl> <dbl> <dbl>    <dbl> <dbl> <dbl>
#>  1 2020-03-16 0-15~ XA         0     0     0     1     0        0     1     1
#>  2 2020-03-16 0-15~ a          0     0     0     0     0        0     0     0
#>  3 2020-03-16 0-15~ b          0     0     0     0     0        0     0     0
#>  4 2020-03-16 0-15~ c          0     0     0     1     0        0     1     1
#>  5 2020-03-16 0-15~ d          0     0     0     0     0        0     0     0
#>  6 2020-03-16 0-15~ SA         3     0     0     0     0        0     3     3
#>  7 2020-03-16 0-15~ a          3     0     0     0     0        0     3     3
#>  8 2020-03-16 0-15~ b          0     0     0     0     0        0     0     0
#>  9 2020-03-16 0-15~ c          0     0     0     0     0        0     0     0
#> 10 2020-03-16 0-15~ d          0     0     0     0     0        0     0     0
#> 11 2020-03-16 0-15~ RE         0     1     1     0     0        0     2     2
#> 12 2020-03-16 0-15~ a          0     0     0     0     0        0     0     0
#> 13 2020-03-16 0-15~ b          0     0     0     0     0        0     0     0
#> 14 2020-03-16 0-15~ c          0     1     1     0     0        0     2     2
#> 15 2020-03-16 0-15~ d          0     0     0     0     0        0     0     0
#> 16 2020-03-16 <NA>  XA         0     0     0     0     1        1     2     2
#> 17 2020-03-16 <NA>  a          0     0     0     0     0        0     0     0
#> 18 2020-03-16 <NA>  b          0     0     0     0     0        1     1     1
#> 19 2020-03-16 <NA>  c          0     0     0     0     0        0     0     0
#> 20 2020-03-16 <NA>  d          0     0     0     0     1        0     1     1
#> 21 2020-03-16 <NA>  RE         0     0     0     0     0        2     2     2
#> 22 2020-03-16 <NA>  a          0     0     0     0     0        0     0     0
#> 23 2020-03-16 <NA>  b          0     0     0     0     0        0     0     0
#> 24 2020-03-16 <NA>  c          0     0     0     0     0        0     0     0
#> 25 2020-03-16 <NA>  d          0     0     0     0     0        2     2     2
#> # ... with 1 more variable: percent <chr>
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.