3

次のようなデータフレームがあります。

created_at  actor_attributes_email      type
3/11/12 7:28    jeremy@asynk.ch         PushEvent
3/11/12 7:28    jeremy@asynk.ch         PushEvent
3/11/12 7:28    jeremy@asynk.ch         PushEvent
3/11/12 7:42    jeremy@asynk.ch         IssueCommentEvent
3/11/12 11:06   d.bussink@gmail.com     PushEvent
3/11/12 11:06   d.bussink@gmail.com     PushEvent

次に、月/年で並べ替えます(時間で並べ替え、行の整合性を維持します)。これにより、各月に3つの列が作成され、その月に関連するすべてのデータ(created_at、actor_attributes_email、およびtype)がこれらの3つの列に配置され、次のヘッダーが取得されます(データに存在するすべての月)。

april_2011_created_at april_2011_actor_attributes_email april_2011_type may_2011_created_at may_2011_actor_attributes_email may_2011_type  

Rでこれをどのように達成できますか?

データセット全体を含むCSVファイルはここにあります: https ://github.com/aronlindberg/VOSS-Sequencing-Toolkit/blob/master/rubinius_rubinius_sequencing/rubinius_6months.csv

dput()CSVの最初の行は次のとおりです。

structure(list(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 07:28:04", 
"2012-03-11 07:28:19", "2012-03-11 07:42:16", "2012-03-11 11:06:13", 
"2012-03-11 12:46:25", "2012-03-11 13:03:12", "2012-03-11 13:12:34", 
"2012-03-11 13:14:52", "2012-03-11 13:30:14", "2012-03-11 13:30:48"
), class = "factor"), actor_attributes_email = structure(c(3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", 
"d.bussink@gmail.com", "jeremy@asynk.ch"), class = "factor"), 
    type = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L), .Label = c("IssueCommentEvent", "PushEvent"
    ), class = "factor")), .Names = c("created_at", "actor_attributes_email", 
"type"), class = "data.frame", row.names = c(NA, -30L))

他のいくつかの仮定は次のとおりです。

  • 「PushEvent」(たとえば)が10回繰り返されても、RパッケージTraMineRを使用して配列分析を行うため、これらすべてを保持する必要があります。
  • 列の長さが等しくない場合があります
  • 異なる月の列間には関係がありません
  • 特定の月内のデータは、最も早い時間で最初に並べ替える必要があります
  • たとえば、2011年6月と2012年6月のデータは別々の列に入れる必要があります
4

2 に答える 2

4

マイアサウラは、プライヤーとルブリデートで仕事を成し遂げるためのエレガントな方法を提供しました。これは、ベースRでそれを達成するための少しエレガントでない方法です。しかし、マイアサウラの方法とは異なり、この方法はNA行数を最小限に抑えます。各月の行数NAは、その月の行数と任意の月の最大行数の差です。

# split df by month
by.mon <- split(df, months(as.POSIXct(df$created_at)))

# rename the columns to include the month name
by.mon <- mapply(
    function(x, mon.name) {
        names(x) <- paste(mon.name, names(x), sep='_');
        return(x)
    }, x=by.mon, mon.name=names(by.mon), SIMPLIFY=FALSE)

# add an index column for merging on
by.mon.indexed <- lapply(by.mon, function(x) within(x, index <- 1:nrow(x)))

# merge all of the months together
results <- Reduce(function(x, y) merge(x, y, by='index', all=TRUE, sort=FALSE), 
    by.mon.indexed)

# remove the index column
final_result <- results[names(results) != 'index']
于 2012-10-10T17:06:47.353 に答える
4
library(plyr)
library(lubridate)
df$created_at <- ymd_hms(df$created_at, quiet = TRUE)
df$mname <- as.character(lubridate::month(df$created_at,label = T, abbr = T))
result <- dlply(df, .(mname), function(x){
      x <- arrange(x, created_at)
      names(x) <- paste0(unique(x$mname), "_", names(x))
      x$mname <- NULL
      x
    }, .progress = 'text')

final_result <- ldply(result, rbind.fill)[, -1]

3つの列名に月の名前を追加し、適切なデータを入力する必要があるため、データがないすべての列にsが入力されることに注意してくださいNA(これはの意図された動作ですrbind.fill)。

于 2012-10-10T16:08:47.897 に答える