繁体   English   中英

取消按行分组的r中的数据帧

[英]ungrouping dataframe in r which is grouped by rows

我有这个数据框

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"))

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

 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)

正如-pkumar在评论中建议的那样,我使用tidyverse的unnest完成了此操作

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM