2017-11-11 173 views
1

我有一個數據幀提取出位置索引且請求對得到匹配/在Web訪問會話發生

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)爲示範目的的實例。

所需的輸出: -

enter image description here

如果可能的話,我想篩選出那些session_ids了來港。因此,同時具有匹配位置1和位置2只session_ids切不可NA

+0

是子集(lf1,!any(is.na(positions)))'你需要什麼? –

+1

您是否認爲通過運行lf1代碼可以看到'lf%>%group_by(session_id)%>%filter(all(!is.na(match(c(1,6),request))))' – akrun

+0

如果從lf1%>%mutate(position1 = someCodeToFetchPos1,position2 = FetchPos2) – SumitArya

回答

2

如果我們需要從「LFL」總的輸出,ungroup數據,然後filter出具有任何NA list元素行和mutate打造「位置1」和'position2'

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 
+1

是的。謝謝akrun.God祝福你! – SumitArya

+0

是的,就是這樣。謝謝akrun.God祝福你! – SumitArya

+0

@SumitArya謝謝你,很高興幫助你。上帝保佑你! – akrun

相關問題