使用R将CSV文件切割成不同的列

时间:2012-08-26 16:18:15

标签: r csv data-manipulation traminer

这是Pivoting a CSV file using R的后续问题。

在那个问题中,我想根据列(repository_name)中的值将单个列(类型)切割成多个列。使用了以下输入数据。

                 type          created_at repository_name
1         IssuesEvent 2012-03-11 06:48:31       bootstrap
2         IssuesEvent 2012-03-11 06:48:31       bootstrap
3   IssueCommentEvent 2012-03-11 07:03:57       bootstrap
4   IssueCommentEvent 2012-03-11 07:03:57       bootstrap
5   IssueCommentEvent 2012-03-11 07:03:57       bootstrap
6        IssuesEvent 2012-03-11 07:03:58        bootstrap
7         WatchEvent 2012-03-11 07:18:45        hogan.js
8         WatchEvent 2012-03-11 07:18:45        hogan.js
9         WatchEvent 2012-03-11 07:18:45        hogan.js
10   IssueCommentEvent 2012-03-11 07:03:57      bootstrap

完整的CSV文件位于https://github.com/aronlindberg/VOSS-Sequencing-Toolkit/blob/master/twitter_exploratory_analysis/all_events.csv

这是CSV的前30行的dput():

structure(list(type = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 2L, 
2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 
1L, 4L, 4L, 4L, 2L, 2L, 2L), .Label = c("ForkEvent", "IssueCommentEvent", 
"IssuesEvent", "WatchEvent"), class = "factor"), created_at = structure(c(1L, 
1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 
6L, 7L, 7L, 7L, 8L, 8L, 8L, 9L, 9L, 9L, 10L, 10L, 10L), .Label = c("2012-03-11 06:48:31", 
"2012-03-11 06:52:50", "2012-03-11 07:03:57", "2012-03-11 07:03:58", 
"2012-03-11 07:15:44", "2012-03-11 07:18:45", "2012-03-11 07:19:01", 
"2012-03-11 07:23:56", "2012-03-11 07:32:43", "2012-03-11 07:38:52"
), class = "factor"), repository_name = structure(c(1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 
1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 1L, 1L, 1L), .Label = c("bootstrap", 
"hogan.js", "twemproxy"), class = "factor")), .Names = c("type", 
"created_at", "repository_name"), class = "data.frame", row.names = c(NA, 
-30L))

提出此代码的@flodel很好地回答了这个问题。

data.split <- split(events.raw$type, events.raw$repository_name)
data.split

list.to.df <- function(arg.list) {
  max.len  <- max(sapply(arg.list, length))
  arg.list <- lapply(arg.list, `length<-`, max.len)
  as.data.frame(arg.list)
}

df.out <- list.to.df(data.split)
df.out

但是,现在我想对列表进行排序,以便每个repo(repository_name)的事件(类型)每月按行排序(从“created_at”列中提取),如下所示:

    bootstrap_2012_03   bootstrap_2012_04    hogan.js_2012_03
1    IssuesEvent          PushEvent          PushEvent
2    IssuesEvent          IssuesEvent        IssuesEvent
3    OssueCommentEvent    WatchEvent         IssuesEvent

其他一些假设是:

  • 时间戳仅用于订购,不需要跨行同步
  • 即使“IssuesEvent”重复10次,我也需要保留所有这些,因为我将使用R包TraMineR进行序列分析
  • 列长度可以不等
  • 不同的repos(“repository_name”)
  • 的列之间没有关系
  • 同一存储库的不同月份的数据完全独立

我如何在R?

中完成此任务

1 个答案:

答案 0 :(得分:3)

不是按repository_name列拆分,而是首先创建一个将repository_name和月份组合在一起的新列:

events.raw$month      <- format(as.Date(events.raw$created_at), "%Y_%m")
events.raw$repo.month <- paste(events.raw$repository_name,
                               events.raw$month, sep = "_")

head(events)
#          type          created_at repository_name   month        repo.month
# 1 IssuesEvent 2012-03-11 06:48:31       bootstrap 2012_03 bootstrap_2012_03
# 2 IssuesEvent 2012-03-11 06:48:31       bootstrap 2012_03 bootstrap_2012_03
# 3 IssuesEvent 2012-03-11 06:48:31       bootstrap 2012_03 bootstrap_2012_03
# 4 IssuesEvent 2012-03-11 06:52:50       bootstrap 2012_03 bootstrap_2012_03
# 5 IssuesEvent 2012-03-11 06:52:50       bootstrap 2012_03 bootstrap_2012_03
# 6 IssuesEvent 2012-03-11 06:52:50       bootstrap 2012_03 bootstrap_2012_03

然后使用我上次建议的相同方法:

data.split <- split(events.raw$type, events.raw$repo.month)

list.to.df <- function(arg.list) {
  max.len  <- max(sapply(arg.list, length))
  arg.list <- lapply(arg.list, `length<-`, max.len)
  as.data.frame(arg.list)
}

df.out <- list.to.df(data.split)
head(df.out)
#    bootstrap_2012_03 hogan.js_2012_03 twemproxy_2012_03
# 1        IssuesEvent       WatchEvent        WatchEvent
# 2        IssuesEvent       WatchEvent        WatchEvent
# 3        IssuesEvent       WatchEvent        WatchEvent
# 4        IssuesEvent             <NA>              <NA>
# 5        IssuesEvent             <NA>              <NA>
# 6        IssuesEvent             <NA>              <NA>