如何在一定观察之上计算观察值?

时间:2015-04-15 19:03:37

标签: r count dataset

以下是我的数据集的一部分。我有三列(itemid用户名和Bidding_Time),最后一列(先前用户数)是我的目标变量。
在每个itemid 中的每个Bidding_Time观察中,我希望拥有多个先前用户。换句话说,我想在每个Bidding_Time值的正上方计算用户名。我应该怎么做?
(先前用户变量中的一些值由我自己计算,我想填写该变量) 请帮帮我。

 itemid       username           Bidding_Time          # of prior users
 109930                         03FEB15:23:45:02             0
 109930                         04FEB15:21:33:57             0
 109930                         04FEB15:21:42:45             0
 109930       steves22                       
 109930       rubber_c                 
 109930                         04FEB15:22:00:05             2
 109930                         04FEB15:22:00:05             0
 109930                         04FEB15:22:00:05             0
 109930                         04FEB15:22:00:05             0
 109930                         04FEB15:22:00:05             0
 109930                         04FEB15:22:00:05             0
 109930                         04FEB15:22:00:05             0
 109930                         04FEB15:22:00:05             0
 109931                         03FEB15:23:45:22             0
 109931       bacardir                 
 109931                         04FEB15:21:34:30             1
 109931       steves22          04FEB15:21:53:11            ...
 109931       rubber_c                 
 109931                         04FEB15:22:00:35
 109932         ljbinc                 
 109932         ljbinc          04FEB15:00:35:46
 109932         shan              
   ...

dput(head(aa))

    structure(list(itemid = c(109930L, 109930L, 109930L, 109930L, 
109930L, 109930L), username = structure(c(1L, 1L, 1L, 96L, 83L, 
1L), .Label = c("", "734723", "7362", "abcarter", "adnerb", "alikira", 
"allkirk", "ardub", "auctione", "bacardir", "barb70", "beasley", 
"belanger", "beluga", "billygol", "bobwyatt", "buffalo1", "butterfl", 
"bytemong", "camille", "carikas", "carpaw", "cbialobz", "cbx4evr", 
"cdavis", "chiquita", "cinner", "daddygee", "dandelio", "dlt2", 
"doubleea", "e970333", "edinga", "eglass", "fschuld", "gonegolf", 
"lightnin", "lionreen", "ljbinc", "lorac", "lorigala", "mec", 
), class = "factor"), Bidding_Time = structure(c(8L, 145L, 154L, 
1L, 1L, 169L), .Label = c("", "03FEB15:23:19:55", "03FEB15:23:22:13", 
"03FEB15:23:38:48", "03FEB15:23:40:26", "03FEB15:23:43:19", "03FEB15:23:43:39", 
"03FEB15:23:45:02", "03FEB15:23:45:22", "03FEB15:23:46:16", "03FEB15:23:47:43", 
"03FEB15:23:47:57", "03FEB15:23:48:39", "03FEB15:23:52:55", "04FEB15:00:00:09", 
"04FEB15:00:02:41", "04FEB15:00:04:54", "04FEB15:00:06:43", "04FEB15:00:07:27", 
"04FEB15:00:07:54", "04FEB15:00:25:10", "04FEB15:00:25:31", "04FEB15:00:26:48", 
"04FEB15:00:35:46", "04FEB15:00:36:20", "04FEB15:00:36:42", "04FEB15:00:37:32", 
"04FEB15:00:39:01", "04FEB15:00:39:30", "04FEB15:00:39:45", "04FEB15:00:40:17", 
"04FEB15:00:40:42", "04FEB15:00:47:07", "04FEB15:00:47:55", "04FEB15:00:54:04", 
"04FEB15:01:15:37", "04FEB15:09:08:44", "04FEB15:09:43:21", "04FEB15:10:18:51", 
"04FEB15:10:20:44", "04FEB15:10:21:50", "04FEB15:11:11:39", "04FEB15:11:13:54", 
"04FEB15:11:14:41", "04FEB15:11:15:51", "04FEB15:12:04:41", "04FEB15:12:24:11", 
"04FEB15:12:25:24", "04FEB15:12:32:02", "04FEB15:12:33:13", "04FEB15:12:35:42", 
"13FEB15:22:03:55", "13FEB15:22:04:16", "13FEB15:22:04:40", "13FEB15:22:04:57", 
"13FEB15:22:05:29", "13FEB15:22:07:00", "13FEB15:22:07:12", "13FEB15:22:07:34", 
), class = "factor")), .Names = c("itemid", 
"username", "Bidding_Time"), row.names = c(NA, 6L), class = "data.frame")

2 个答案:

答案 0 :(得分:1)

效率不高:

install.packages("dplyr") #only once
library(dplyr)
bb <- aa

bb$temp1 <- (bb$Bidding_Time == "")*1
bb$temp2 <- 1

for(i in 2:dim(bb)[1]){
    if(bb$temp1[i]==bb$temp1[i-1]) {
       bb$temp2[i] <- bb$temp2[i-1]
    } else {
       bb$temp2[i] <- bb$temp2[i-1]+1
    }
}

bb <- bb %>% group_by(itemid, temp2) %>% mutate(Count=cumsum(temp1)) %>% 
  ungroup %>% mutate(Count=lag(Count)) %>% 
  select(itemid, username, Bidding_Time, Count)

bb$Count[is.na(bb$Count)] <- 0

bb %>% View

答案 1 :(得分:1)

使用rle在这里非常接近,但我无法完成它。也许有人可以为我挑选......

a <- c("", "", "", "A", "A", "", "", "B", "A", "C", "", "")
b <- a!=""
c <- rep(rle(b)$lengths, rle(b)$lengths)
c2 <- c(NA, c[-length(c)])
> cbind(a,c2)
      a   c2 
 [1,] ""  NA 
 [2,] ""  "3"
 [3,] ""  "3"
 [4,] "A" "3"
 [5,] "A" "2"
 [6,] ""  "2"
 [7,] ""  "2"
 [8,] "B" "2"
 [9,] "A" "3"
[10,] "C" "3"
[11,] ""  "3"
[12,] ""  "2"