在r中取消组合数据帧,按行划分

时间:2018-05-06 14:29:03

标签: r dplyr

我有这个数据框

Source: local data frame [159 x 2]
Groups: <by row>

# A tibble: 159 × 2
   session_id requestId
*       <int>    <list>
1        1105 <int [3]>
2        1107 <int [2]>
3        1108 <int [6]>
4        1109 <int [1]>
5        1110 <int [6]>
6        1111 <int [8]>
7        1112 <int [4]>
8        1114 <int [8]>
9        1117 <int [7]>
10       1118 <int [4]>
# ... with 149 more rows

我不知道如何取消组合或不工作,因为它按行而非某些变量分组..

我希望我的输出看起来像给定的模式/格式

 # A tibble: 342 × 2
   session_id requestId
        <int>     <dbl>
1        1105        10
2        1105         3
3        1107        13
4        1107        13
5        1108         4
6        1108         6
7        1109        12
8        1109         5
9        1110         6
10       1110        10

我不知道该怎么做,如果知道就必须简单。谢谢你的帮助

编辑: -

structure(list(session_id = c(1105L, 1107L, 1108L, 1109L, 1110L, 
1111L, 1112L, 1114L, 1117L, 1118L), requestId = list(c(8L, 14L, 
20L), c(7L, 14L), c(1L, 7L, 8L, 20L, 16L, 17L), 8L, c(1L, 16L, 
17L, 8L, 14L, 20L), c(1L, 7L, 8L, 20L, 4L, 11L, 13L, 14L), c(4L, 
11L, 13L, 14L), c(6L, 8L, 14L, 2L, 4L, 10L, 15L, 18L), c(4L, 
5L, 10L, 16L, 2L, 15L, 18L), c(20L, 1L, 7L, 8L))), .Names = c("session_id", 
"requestId"), row.names = c(NA, -10L), class = c("tbl_df", "tbl", 
"data.frame"))

2 个答案:

答案 0 :(得分:0)

这是一个可能的起点,尽管它仍然缺少一个可重复的例子。当我们得到一个时可以编辑:

 tlengths <- sapply( tbl$requestId, length)
#Error in lapply(X = X, FUN = FUN, ...) : object 'tbl' not found
 sessions <- mapply(rep, sessionId, tlengths)
#Error in mapply(rep, sessionId, tlengths) : object 'sessionId' not found
 newtbl <- tibble(session_id=sessions, requestId=unlist(tbl$requestId))
#Error in tibble(session_id = sessions, requestId = unlist(tbl$requestId)) : 
 # could not find function "tibble"
 library(tidyverse)

答案 1 :(得分:0)

正如-pkumar在评论中建议的那样,我使用了tidyverse&#39; s

来做到这一点
df = structure(list(session_id = c(1105L, 1107L, 1108L, 1109L, 1110L, 
1111L, 1112L, 1114L, 1117L, 1118L), requestId = list(c(8L, 14L, 
20L), c(7L, 14L), c(1L, 7L, 8L, 20L, 16L, 17L), 8L, c(1L, 16L, 
17L, 8L, 14L, 20L), c(1L, 7L, 8L, 20L, 4L, 11L, 13L, 14L), c(4L, 
11L, 13L, 14L), c(6L, 8L, 14L, 2L, 4L, 10L, 15L, 18L), c(4L, 
5L, 10L, 16L, 2L, 15L, 18L), c(20L, 1L, 7L, 8L))), .Names = c("session_id", 
"requestId"), row.names = c(NA, -10L), class = c("tbl_df", "tbl", 
"data.frame"))

library('tidyverse')
unnest(df)