簡體   English   中英

如何在某個觀察值之上計算觀察值?

[英]How to count observations above a certain observation?

以下是我的數據集的一部分。 我有三列(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")

它不是很有效:

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

使用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"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM