【问题标题】:count clustered values in csv计算 csv 中的聚集值
【发布时间】:2015-04-02 04:49:20
【问题描述】:

我有一个 csv 文件,其中的行包含一个名称,后跟一系列空值和聚集的实值。

Robert,,,1:00-5:00,1:00-5:00,1:00-5:00,,,,,,2:00-4:00,2:00-4:00,2:00-4:00
John,,,1:00-5:00,1:00-5:00,,,,,,,,,,,,
Casey,,,1:00-5:00,1:00-5:00,1:00-5:00,,,,,,2:00-4:00,2:00-4:00,,,
Sarah,,,1:00-5:00,,,,,,,,2:00-4:00,2:00-4:00,2:00-4:00,,

我想在 R 中编写一个计算集群的脚本。如果行中有三个实际的连续值,那么我想将它们计为“一个”集群。如果有任何少于三个的集群(即一个或两个连续值),那么我想将其算作“一个”单独的集群。

所需的 csv 格式输出:

Robert,2,0
John,0,1
Casey,1,1
Sarah,1,1

comment编辑:
代码导入的 csv 确实有一个标题,但我希望代码忽略标题并从第一行读取(即 Robert,,,1:00-5:00,...)。我还想忽略导入的 csv 文件的最后一列,其中包含每个人工作的总小时数。这是一个带有示例 csv 链接的 github:timeclock_report.csv

Employee,"Mar 23, 2015","Mar 24, 2015","Mar 25, 2015","Mar 26, 2015","Mar 27, 2015","Mar 28, 2015","Mar 29, 2015",total hours
"John Smith",16:35 - 21:17 / 4.7,16:35 - 21:17 / 4.7,16:35 - 21:17 / 4.7,,,,11:17 - 16:08 / 4.85,18.9569
"Emily Smith",,,,,,08:13 - 12:40 / 4.45,,4.4472222222222
"Robert Jenkins",16:54 - 21:11 / 4.29,16:54 - 21:11 / 4.29,,,16:22 - 22:59 / 6.61,,,15.18638
"Rachel Lipscomb",,,,,,13:18 - 19:04 / 5.76,,5.7638888888889
"Donald Driver",,,,,08:13 - 13:05 / 4.86,08:13 - 13:05 / 4.86,10:02 - 16:02 / 6,15.14694

【问题讨论】:

  • 有一些数据要分享吗?简而言之,您可以使用 reshape2 库 df <-read.csv(file)、melt(df, id.var="name")` 来执行此操作,其中 name 或任何您调用的具有 id 的列
  • 您好,感谢您的回复。代码导入的 csv 确实有一个标题,但我希望代码忽略标题并从第一行读取(即 Robert,,,1:00-5:00,...)。我还想忽略导入的 csv 文件的最后一列,其中包含每个人工作的总小时数。请原谅文件格式不佳,这不是最容易使用的。这是一个带有示例 csv 链接的 github:github.com/agrobins/r_IslandCount (timeclock_report.csv)

标签: r csv count gaps-and-islands


【解决方案1】:

对于这个旧问题,这是一个可能的data.table 解决方案

  • fread() 用于读取输入文件,
  • melt() / dcast() 用于整形,
  • rleid() 函数来识别间隙和孤岛。

对于问题中发布的数据集,此代码

library(data.table)
library(magrittr)

fread("input.csv", header = FALSE, na.strings = c(""), fill = TRUE) %>% 
  .[, V1 := forcats::fct_inorder(V1)] %>%  # to keep the original order in dcast() below
  melt(id.var = "V1") %>% 
  setorder(V1, variable) %>% 
  .[, cluster.id := rleid(V1, is.na(value))] %>%
  .[!is.na(value), .N, by = .(V1, cluster.id)] %>% 
  dcast(V1 ~ N < 3, length, value.var = "N") %>% 
  fwrite("output.csv", col.names = FALSE)

根据要求创建 csv 文件:

Robert,2,0
John,0,1
Casey,1,1
Sarah,1,1

In a comment,OP 提供了指向 github 上托管的另一个示例数据集的链接。

稍作修改,

fread("https://raw.githubusercontent.com/agrobins/r_IslandCount/test_files/timeclock_report.csv"
      , drop = "total hours", na.strings = c("")) %>% 
  .[, Employee := forcats::fct_inorder(Employee)] %>%  # to keep the original order in dcast() below
  melt(id.var = "Employee") %>% 
  setorder(Employee, variable) %>% 
  .[, cluster.id := rleid(Employee, is.na(value))] %>% 
  .[!is.na(value), .N, .(Employee, cluster.id)] %>% 
  dcast(Employee ~ N < 3, length, value.var = "N")

我们得到

          Employee FALSE TRUE
1:      John Smith     1    1
2:     Emily Smith     0    1
3:  Robert Jenkins     0    2
4: Rachel Lipscomb     0    1
5:   Donald Driver     1    0

名为 FALSE 的第一个数字列包含由三个或更多连续条目组成的集群的数量,而名为 TRUE 的第二个数字列包含由 1 个或 2 个连续条目组成的集群的数量。

可重现的数据

由于外部网站的链接很脆弱,这里是从
https://raw.githubusercontent.com/agrobins/r_IslandCount/test_files/timeclock_report.csv检索到的第二个数据集的副本

Employee,"Mar 23, 2015","Mar 24, 2015","Mar 25, 2015","Mar 26, 2015","Mar 27, 2015","Mar 28, 2015","Mar 29, 2015",total hours
"John Smith",16:35 - 21:17 / 4.7,16:35 - 21:17 / 4.7,16:35 - 21:17 / 4.7,,,,11:17 - 16:08 / 4.85,18.9569
"Emily Smith",,,,,,08:13 - 12:40 / 4.45,,4.4472222222222
"Robert Jenkins",16:54 - 21:11 / 4.29,16:54 - 21:11 / 4.29,,,16:22 - 22:59 / 6.61,,,15.18638
"Rachel Lipscomb",,,,,,13:18 - 19:04 / 5.76,,5.7638888888889
"Donald Driver",,,,,08:13 - 13:05 / 4.86,08:13 - 13:05 / 4.86,10:02 - 16:02 / 6,15.14694

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-03
    • 1970-01-01
    • 2011-11-27
    • 1970-01-01
    • 2021-09-19
    • 1970-01-01
    • 2023-02-05
    • 1970-01-01
    相关资源
    最近更新 更多