Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 提取web访问会话中请求对匹配/出现的位置索引_R_Dplyr_Plyr - Fatal编程技术网

R 提取web访问会话中请求对匹配/出现的位置索引

R 提取web访问会话中请求对匹配/出现的位置索引,r,dplyr,plyr,R,Dplyr,Plyr,我有一个数据帧的子集 lf = structure(list(session_id = c(48L, 48L, 48L, 48L, 48L, 48L, 54L, 54L, 54L, 54L, 54L, 54L, 72L, 72L, 72L, 72L, 72L, 74L, 74L, 74L, 74L, 74L, 78L, 78L, 78L, 78L, 78L, 90L, 90L, 90L), datetime = structure(c(1457050110, 1457050111, 145

我有一个数据帧的子集

lf = structure(list(session_id = c(48L, 48L, 48L, 48L, 48L, 48L, 54L, 
54L, 54L, 54L, 54L, 54L, 72L, 72L, 72L, 72L, 72L, 74L, 74L, 74L, 
74L, 74L, 78L, 78L, 78L, 78L, 78L, 90L, 90L, 90L), datetime = structure(c(1457050110, 
1457050111, 1457050112, 1457050114, 1457050117, 1457050118, 1457052045, 
1457052048, 1457052050, 1457052051, 1457052052, 1457052054, 1457057067, 
1457057067, 1457057067, 1457057070, 1457057071, 1457058143, 1457058143, 
1457058144, 1457058149, 1457058150, 1457059193, 1457059193, 1457059195, 
1457059198, 1457059199, 1457063485, 1457063486, 1457063486), class = c("POSIXct", 
"POSIXt"), tzone = "UTC"), request = c(7, 7, 14, 20, 9, 4, 9, 
1, 12, 20, 6, 12, 4, 15, 8, 8, 12, 10, 6, 6, 13, 1, 5, 6, 20, 
1, 8, 3, 6, 13)), .Names = c("session_id", "datetime", "request"
), row.names = c(NA, -30L), class = c("grouped_df", "tbl_df", 
"tbl", "data.frame"))
现在,我正在检查给定数据帧中的某对请求(req1、req2)是否匹配,如果存在匹配,则获取发生匹配的位置索引

我正在使用这段代码:-

lf1 = lf %>% group_by(session_id) %>% do(positions = match(c(1,6),.$request)) 
如您所见,我以请求对(1,6)为例进行演示

期望输出:-


如果可能的话,我想过滤掉那些会话ID。因此,只有同时具有匹配位置1和位置2的session_id不能是NA

如果我们需要来自“lfl”的汇总输出,
解组
数据,然后
筛选出具有任何NA和
变异
元素的
行,以创建“位置1”和“位置2”

library(dplyr)
library(purrr)
lf1 %>%
   ungroup %>% 
   filter(map_lgl(positions, ~all(!is.na(.)))) %>%
   mutate(position1 = map_int(positions, ~.[1]), position2 = map_int(positions, ~.[2]))
# A tibble: 3 x 4
#  session_id positions position1 position2
#        <int>    <list>     <int>     <int>
#1         54 <int [2]>         2         5
#2         74 <int [2]>         5         2
#3         78 <int [2]>         4         2
库(dplyr)
图书馆(purrr)
lf1%>%
解组%>%
过滤器(映射lgl(位置,~all(!is.na()))%>%
变异(位置1=映射int(位置,~[1]),位置2=映射int(位置,~[2]))
#一个tibble:3x4
#会话id位置1位置2
#                      
#1         54          2         5
#2         74          5         2
#3         78          4         2

Is
subset(lf1,!any(Is.na(positions)))
您需要什么?您的意思是
lf%>%groupby(session_id)%%>%filter(all(!Is.na(match(c(1,6),request)))
正如您所看到的,通过运行lf1代码,我们得到了包含第一次匹配的位置的列表。现在从lf1%>%mutate(position1=somecodetchpos1,pos2=FetchPos2)或者可能是
lf1%>%ungroup%>%filter(map\u lgl(positions,~all(!is.na()))%%>%变异(position1=map\u chr(positions,~[1]),position2=map\u chr(positions,~[2])