簡體   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