【问题标题】:Combine multiple data frames in r for bar chart race在 r 中组合多个数据框以进行条形图竞赛
【发布时间】:2020-11-13 05:54:53
【问题描述】:

我有大约 15 个数据框,每个数据框类似于以下示例。每个数据框代表机器在 2019-07-01 到 2020-06-30 的 12 个月内某一天发生的故障数

Machine #1
#        date         n
# 1      2019-07-01   8
# 2      2019-07-02   6
# 3      2019-07-03   10
# 4      2019-07-05   1
# 5      2019-07-06   2
# 6      2019-07-08   6
# .      ..........   .
# n      2020-06-30   5

我想在 Flourish Studio 使用这个工具 here 来创建条形图比赛,但需要我的数据是表格;

  Machine Number    2019-07-01   2019-07-02   2019-07-03   2019-07-04   2019-07-05   ......   2020-06-30
# 1     1                8            6            10           0            1                     5
# 2     2                0            4            3            1            0                     3 
# 3     3                2            0            1            3            5                     6
# 4     4                3            1            0            0            9                     11
# .     .                .            .            .            .            .                     .
# 15    15               10           8            4            6            0                     1

所以基本上我需要得到一个 15 行 366 列的数据框(一个用于机器编号,365 用于一年中的每一天)

需要注意的重要一点是,如果在给定的一天没有报告任何故障,则不会记录任何数据,换句话说,任何数据帧中都没有零,因此需要以某种方式在需要的地方添加它们。

我是一个绝对的初学者,我花了数周时间才将数据整理成当前的形式,此时我不知道如何继续。因此,我们将不胜感激。

谢谢。

【问题讨论】:

  • Base R rbinddplyr::bind_rows df,然后是 reshape data from long to wide in R。这似乎是重复的,如果是这样的话,我会关闭它。
  • df 是否在列表中,在.GlobalEnv 中,在哪里?
  • @RuiBarradas 是的,我在Global.Env 中有 15 个 df

标签: r dataframe bar-chart


【解决方案1】:

问题中的问题是通过一系列步骤解决的,其中大多数已经在 SO 的其他地方得到了回答,但是就在这里。

# Get the data.frames into a list
machine_names <- ls(pattern = "^Machine")
machine_list <- mget(machine_names, envir = .GlobalEnv)

# Coerce the date column to class "Date"
# and add a small amount of noise to 'n'
set.seed(2020)
machine_list <- lapply(machine_list, function(x){
  x[["date"]] <- as.Date(x[["date"]])
  x[["n"]] <- x[["n"]] + sample(-2:10, length(x[["n"]]), TRUE)
  x
})

# Create a column with the data.frame number
machine_list <- lapply(seq_along(machine_list), function(i){
  cbind.data.frame(Machine_Number = i, machine_list[[i]])
})
# Put all data.frames in the same df
machines <- do.call(rbind, machine_list)

# Now reshape from long to wide format
library(dplyr)
library(tidyr)    

# Arrange data according do date so pivot_wider gives
# the correct column order.
machines <- machines %>% arrange(date)

machines %>%
  pivot_wider(
    id_cols = Machine_Number,
    names_from = date,
    values_from = n,
    values_fill = 0
  )

数据创建代码

temp <- read.table(text = "
        date         n
 1      2019-07-01   8
 2      2019-07-02   6
 3      2019-07-03   10
 4      2019-07-05   1
 5      2019-07-06   2
 6      2019-07-08   6
", header = TRUE)

machine_list <- replicate(15, temp, simplify = FALSE)
names(machine_list) <- paste("Machine", seq_along(machine_list), sep = "_")
list2env(machine_list, envir = .GlobalEnv)
rm(machine_list)

【讨论】:

  • 哇,感谢您的深入回复。这对我来说看起来很吓人,但我会努力解决的,让你知道我是怎么走的。再次感谢。
猜你喜欢
  • 2017-02-22
  • 1970-01-01
  • 2021-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多