使用R

时间:2016-02-26 06:19:27

标签: r

我在R'HLS'中有一个数据框,当访问者访问网站时,它基本上是页面明细的细节。如果他已经移动到page_count表示的第10页,则每行代表从第3页开始到最多10页的每页访问。

ID   page_count   purchase_flag   prob     hl_flag
V1      3              1          0.76       1
V1      4              1          0.65       1
V1      5              1          0.04       0
V1      6              1          0.86       1
V1      7              1          0.04       0
V1      8              1          0.65       1
V1      9              1          0.01       0
V1      10             1          0.00       0
V2      3              0          0.03       0
V2      4              0          0.01       0
V2      5              0          0.02       0
V2      6              0          0.00       0
V3      3              1          0.02       0
V3      4              1          0.001      0
V3      5              1          0.76       1
V3      6              1          0.03       0
V4      3              0          0.04       0
V4      4              0          0.65       1
V4      5              0          0.03       0 

我想创建一个表,它将获取行直到第一次出现hl_flag = 1(如果该情况为真)并且如果hl_flag = 0则所有行都为任何ID。输出需要看起来像这样

ID     page_count     purchase_flag    prob     hl_flag
V1         3                1          0.76      1
V2         3                0          0.03      0
V2         4                0          0.01      0
V2         5                0          0.02      0
V2         6                0          0.00      0
V3         3                1          0.02      0
V3         4                1          0.001     0
V3         5                1          0.76      1
V4         3                0          0.04      0
V4         4                0          0.65      1

提前感谢您的帮助。

更新: 添加dput的输出如下

structure(list(ung_id = c("00000f23-1019-4aff-8199-35bd0d032356/1", 
"00000f23-1019-4aff-8199-35bd0d032356/1", "00000f23-1019-4aff-8199-35bd0d032356/1", 
"00000f23-1019-4aff-8199-35bd0d032356/1", "00002b20-82d4-497b-a137-34e3bb4eaf74/1", 
"00002b20-82d4-497b-a137-34e3bb4eaf74/1", "00002b20-82d4-497b-a137-34e3bb4eaf74/1", 
"0000aeff-2d8b-4daa-a084-fb2980f1feed/1", "0000aeff-2d8b-4daa-a084-fb2980f1feed/1", 
"0000b96e-566f-4b6e-925a-b7dcfd4a7208/1", "0000b96e-566f-4b6e-925a-b7dcfd4a7208/1", 
"0000b96e-566f-4b6e-925a-b7dcfd4a7208/1", "0000b96e-566f-4b6e-925a-b7dcfd4a7208/1", 
"0000b96e-566f-4b6e-925a-b7dcfd4a7208/1", "0000b96e-566f-4b6e-925a-b7dcfd4a7208/1", 
"0000b96e-566f-4b6e-925a-b7dcfd4a7208/1", "0000b96e-566f-4b6e-925a-b7dcfd4a7208/1", 
"0000d089-edda-4c8b-8b17-d9def3cae7cf/1", "0000d089-edda-4c8b-8b17-d9def3cae7cf/1", 
"0000d089-edda-4c8b-8b17-d9def3cae7cf/1"), nop_count = c(3L, 
4L, 5L, 6L, 3L, 4L, 5L, 3L, 4L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
3L, 4L, 5L), purchase_flag = c(1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), prob = c(0.0777615841278747, 
0.0738346887497272, 0.0741130887754292, 0.0785370078084892, 0.0619573259953132, 
0.0516201527986966, 0.0562025814090338, 0.0837301511694211, 0.0579033581198143, 
0.0364358545936557, 0.0329682922619259, 0.0420157964561273, 0.049855260762479, 
0.0500481302257314, 0.0463893143028813, 0.049855260762479, 0.0391886960037603, 
0.0683568422952682, 0.0570168506417919, 0.0661965354597502), 
decile = structure(c(8L, 8L, 8L, 8L, 6L, 4L, 5L, 8L, 5L, 
1L, 1L, 2L, 4L, 4L, 3L, 4L, 2L, 7L, 5L, 7L), .Label = c("(0.0257,0.0364]", 
"(0.0364,0.0428]", "(0.0428,0.0482]", "(0.0482,0.0531]", 
"(0.0531,0.0583]", "(0.0583,0.0645]", "(0.0645,0.0722]", 
"(0.0722,0.0842]"), class = "factor"), hl_Flag = c(1L, 1L, 
1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
1L, 1L, 1L)), .Names = c("ung_id", "nop_count", "purchase_flag", 
"prob", "decile", "hl_Flag"), row.names = c(NA, -20L), .internal.selfref = <pointer: 0x00000000002b0788>, class = c("data.table", 
"data.frame"))

2 个答案:

答案 0 :(得分:2)

一个选项是使用data.table。我们将'data.frame'转换为'data.table'(setDT(HLS)),按'ID'分组,我们检查是否有any'hl_flag'值为1.在这种情况下,我们使用which.max得到hl_flag中第一次出现1的索引,得到序列(1:(which.max..)),找到行索引(.I)或else只返回行索引(.I),提取具有行索引($V1)的列,并使用它来对行进行子集化。

library(data.table)
setDT(HLS)[HLS[, if(any(hl_flag==1)) .I[1:(which.max(hl_flag))]
               else .I, ID]$V1]
#     ID page_count purchase_flag  prob hl_flag
# 1: V1          3             1 0.760       1
# 2: V2          3             0 0.030       0
# 3: V2          4             0 0.010       0
# 4: V2          5             0 0.020       0
# 5: V2          6             0 0.000       0
# 6: V3          3             1 0.020       0
# 7: V3          4             1 0.001       0
# 8: V3          5             1 0.760       1
# 9: V4          3             0 0.040       0
#10: V4          4             0 0.650       1

或类似于I showed的方法data.tablebase R选项将

do.call(rbind, lapply(split(HLS, HLS$ID), 
           function(x) if(any(x$hl_flag==1)) 
                  x[seq(which.max(x$hl_flag)), ] 
                else x))

或使用dplyr

library(dplyr)
HLS %>% 
    group_by(ID) %>% 
    filter(all(!hl_flag)| row_number() %in% seq(which.max(hl_flag)))
 #      ID page_count purchase_flag  prob hl_flag
 #    (chr)      (int)         (int) (dbl)   (int)
 #1     V1          3             1 0.760       1
 #2     V2          3             0 0.030       0
 #3     V2          4             0 0.010       0
 #4     V2          5             0 0.020       0
 #5     V2          6             0 0.000       0
 #6     V3          3             1 0.020       0
 #7     V3          4             1 0.001       0
 #8     V3          5             1 0.760       1
 #9     V4          3             0 0.040       0
 #10    V4          4             0 0.650       1

答案 1 :(得分:0)

你可以尝试,

l <- lapply(split(df, df$ID), function(x) {if(any(x[5] == 1)) x[1:which.max(x[5] == 1),] else x})

df拆分为id,然后将每个列表分组到任何hl_flag == 1 哪个会给你一个明智的名单

#$V1
#  ID page_count purchase_flag prob hl_flag
#1 V1          3             1 0.76       1

#$V2
#   ID page_count purchase_flag prob hl_flag
#9  V2          3             0 0.03       0
#10 V2          4             0 0.01       0
#11 V2          5             0 0.02       0
#12 V2          6             0 0.00       0

#$V3
#   ID page_count purchase_flag  prob hl_flag
#13 V3          3             1 0.020       0
#14 V3          4             1 0.001       0
#15 V3          5             1 0.760       1

#$V4
#   ID page_count purchase_flag prob hl_flag
#17 V4          3             0 0.04       0
#18 V4          4             0 0.65       1

要获得预期结果,您可以使用bind_rows

中的dplyr
library(dplyr)
bind_rows(l)

#ID page_count purchase_flag  prob hl_flag
#(fctr)      (int)         (int) (dbl)   (int)
#1      V1          3             1 0.760       1
#2      V2          3             0 0.030       0
#3      V2          4             0 0.010       0
#4      V2          5             0 0.020       0
#5      V2          6             0 0.000       0
#6      V3          3             1 0.020       0
#7      V3          4             1 0.001       0
#8      V3          5             1 0.760       1
#9      V4          3             0 0.040       0
#10     V4          4             0 0.650       1