3

我有 97M 行的长表。每行包含一个人所采取的行动的信息和该行动的时间戳,格式如下:

actions <- c("walk","sleep", "run","eat")
people <- c("John","Paul","Ringo","George")
timespan <- seq(1000,2000,1)

set.seed(28100)
df.in <- data.frame(who = sample(people, 10, replace=TRUE),
                    what = sample(actions, 10, replace=TRUE),
                    when = sample(timespan, 10, replace=TRUE))

df.in
#       who  what when
# 1    Paul   eat 1834
# 2    Paul sleep 1295
# 3    Paul   eat 1312
# 4   Ringo   eat 1635
# 5    John sleep 1424
# 6  George   run 1092
# 7    Paul  walk 1849
# 8    John   run 1854
# 9  George sleep 1036
# 10  Ringo  walk 1823

每个动作都可以由一个人采取或不采取,并且可以以任何顺序采取行动。

我有兴趣总结我的数据集中的动作顺序。特别是对于每个人,我想找出首先、第二、第三和第四次采取的行动。如果多次采取行动,我只对第一次发生感兴趣。然后,如果有人跑、吃、吃、跑和睡觉,我有兴趣总结一下,比如run, eat, sleep

df.out <- data.frame(who = factor(character(), levels=people),
                     action1 = factor(character(), levels=actions),
                     action2 = factor(character(), levels=actions),
                     action3 = factor(character(), levels=actions),
                     action4 = factor(character(), levels=actions))

我可以通过 forloop 获得我想要的东西:

for (person in people) {
  tmp <- subset(df.in, who==person)
  tmp <- tmp[order(tmp$when),]
  chrono_list <- unique(tmp$what)
  df.out <- rbind(df.out, data.frame(who = person,
                                     action1 = chrono_list[1],
                                     action2 = chrono_list[2],
                                     action3 = chrono_list[3],
                                     action4 = chrono_list[4]))
}

df.out
#        who action1 action2 action3 action4
#   1   John   sleep     run    <NA>    <NA>
#   2   Paul   sleep     eat    walk    <NA>
#   3  Ringo     eat    walk    <NA>    <NA>
#   4 George   sleep     run    <NA>    <NA>

是否也可以在没有循环的情况下以更有效的方式获得此结果?

4

4 回答 4

5

我们可以dcast从开发版本中使用data.table,即。v1.9.5. 我们可以从here

library(data.table)#v1.9.5+
dcast(setDT(df.in)[order(when),action:= paste0('action', 1:.N) ,who],
                           who~action, value.var='what')

如果您需要unique每个“谁”的“什么”

dcast(setDT(df.in)[, .SD[!duplicated(what)], who][order(when),
    action:= paste0('action', 1:.N), who], who~action, value.var='what')
#         who action1 action2 action3
#1: George   sleep     run      NA
#2:   John   sleep     run      NA
#3:   Paul   sleep     eat    walk
#4:  Ringo     eat    walk      NA

或者使用.I会更快一些

 ind <- setDT(df.in)[,.I[!duplicated(what)], who]$V1 

 dcast(df.in[ind][order(when),action:= paste0('action', 1:.N) ,who], 
            who~action, value.var='what')

或者使用setorderandunique这可能是一种内存效率,因为setorder通过引用重新排序数据集。

 dcast(unique(setorder(setDT(df.in), who, when), by=c('who', 'what'))[,
     action:= paste0('action', 1:.N), who], who~action, value.var='what')
 #     who action1 action2 action3
 #1: George   sleep     run      NA
 #2:   John   sleep     run      NA
 #3:   Paul   sleep     eat    walk
 #4:  Ringo     eat    walk      NA
于 2015-05-13T16:14:48.357 回答
3

您也可以使用组合dplyr+tidyr

library(dplyr)
library(tidyr)

df.in %>%
  group_by(who) %>%
  mutate(when = rank(when), when = paste0("action", when)) %>%
  spread(key = when, value = what)
 ##      who action1 action2 action3 action4
 ## 1 George   sleep     run      NA      NA
 ## 2   John   sleep     run      NA      NA
 ## 3   Paul   sleep     eat     eat    walk
 ## 4  Ringo     eat    walk      NA      NA

编辑

如果您只需要第一次出现的what列,您可以先过滤数据

df.in %>%
  arrange(when) %>%
  group_by(who) %>%
  filter(!duplicated(what)) %>%
  mutate(when = rank(when), when = paste0("action", when)) %>%
  spread(key = when, value = what)
##      who action1 action2 action3
## 1 George   sleep     run      NA
## 2   John   sleep     run      NA
## 3   Paul   sleep     eat    walk
## 4  Ringo     eat    walk      NA
于 2015-05-13T16:20:47.773 回答
0

我看到您已标记 plyr,但您也可以使用 dplyr 执行此操作。像下面这样的东西应该可以工作:

df.in %>%
    group_by(who) %>%
    arrange(when) %>%
    summarise(action1 = first(what),
              action2 = nth(what, 2),
              action3 = nth(what, 3),
              action4 = last(what))
于 2015-05-13T16:19:44.920 回答
0

这是一种使用更传统的方法split-apply-combine。它比for循环更惯用的 R 代码,尽管 {dplyr} 和 {data.table} 解决方案似乎比这种类型的 {base} R 解决方案更常见。此方法使用dcast来自 {reshape2},但它也可以reshape()用于纯粹的 {base} R 解决方案。

这种方法可能不会比for问题中给出的循环快多少。我很想知道给定的三种方法如何比较大型数据集。我是初学者,最近一直在学习 R 数据操作。欢迎任何反馈。

library(reshape2)

#Split the data by person and apply the function
actions <- lapply(split(df.in, df.in$who), function(tmp) {

    tmp <- tmp[order(tmp$when),]
    dup <- duplicated(tmp$what)
    df.out <- data.frame(who = tmp$who[!dup], what = tmp$what[!dup])
    df.out$actionNo <- paste("action", c(1:nrow(df.out)), sep = "")
    return(df.out)

})

#Combine the results
act_rbind <- do.call(rbind, actions)
act_cast <- dcast(act_rbind, who ~ actionNo, value.var = "what")
print(act_cast)

    #      who action1 action2 action3
    # 1 George   sleep     run    <NA>
    # 2   John   sleep     run    <NA>
    # 3   Paul   sleep     eat    walk
    # 4  Ringo     eat    walk    <NA>
于 2015-05-14T04:39:53.940 回答