gpt4 book ai didi

r - pivot_wider 输出 objects

转载 作者:行者123 更新时间:2023-12-04 12:33:05 25 4
gpt4 key购买 nike

我有以下我试图传播的数据集。

    #create df    
df <- structure(list(file_number = c("3098129", "3096451", "3096774",
"3095276", "3095464", "3096846", "3097132", "3096355", "3096951",
"3096328", "3095441", "3096325", "3094412", "3096366", "3096372",
"3096507", "3098510", "3096335", "3096403", "3094343", "3096941",
"3096419", "3094431", "3096495", "3094647", "3094487", "3094947",
"3094398", "3094386", "3094367", "3097480", "3096425", "3095193",
"3095839a", "3097197", "3098453", "3098549", "3098428", "3096427",
"3096895", "3096434", "3094835", "3096312", "3094517", "3094372",
"3096387", "3096480", "3098504", "3096338", "3094615", "3096382",
"3096638", "3096750", "3096418", "3094734", "3098503", "3096311",
"3097197", "3094353", "3098442", "3097111", "3097325", "3096531",
"3096405", "3096301", "3096692", "3096495", "3098406", "3098422",
"3096315", "3096951", "3094491", "3096304", "3098416", "3096332",
"3098404", "3098419", "3095225", "3094404", "3096374", "3098411",
"3098556", "3096398", "3094421b", "3098477", "3094369b", "3098463",
"3096893", "3098514", "3098477", "3098465", "3094560", "3098409",
"3096434", "3097557", "3095061", "3098419", "3096404", "3095441",
"3096537", "3098503", "3098400", "3097808", "3096389b", "3098446",
"3096330", "3095533", "3094421a", "3094339", "3095578", "3094404",
"3098552", "3098514", "3096630", "3096941", "3097027", "3096322",
"3096514", "3098484", "3097038", "3096672", "3098483", "3094373",
"3096774", "3096677", "3096408", "3096664", "3096365", "3096491",
"3096820", "3096514", "3096556", "3096292", "3096495", "3094781",
"3094344", "3094487", "3094690", "3098504", "3096503"), reader = c("aa",
"aa", "aa", "aa", "aa", "aa", "aa", "aa", "aa", "aa", "aa", "aa",
"aa", "aa", "aa", "aa", "aa", "aa", "aa", "aa", "ae", "ae", "ae",
"ae", "ae", "ae", "ae", "ae", "ae", "ae", "ae", "ae", "ae", "ae",
"ae", "ae", "ae", "ae", "ae", "ae", "db", "db", "db", "db", "db",
"db", "db", "db", "db", "db", "db", "db", "db", "db", "db", "db",
"db", "db", "db", "db", "dl", "dl", "dl", "dl", "dl", "dl", "dl",
"dl", "dl", "dl", "dl", "dl", "dl", "dl", "dl", "dl", "dl", "dl",
"dl", "dl", "mk", "mk", "mk", "mk", "mk", "mk", "mk", "mk", "mk",
"mk", "mk", "mk", "mk", "mk", "mk", "mk", "mk", "mk", "mk", "mk",
"mm", "mm", "mm", "mm", "mm", "mm", "mm", "mm", "mm", "mm", "mm",
"mm", "mm", "mm", "mm", "mm", "mm", "mm", "mm", "mm", "np", "np",
"np", "np", "np", "np", "np", "np", "np", "np", "np", "np", "np",
"np", "np", "np", "np", "np", "np", "np"), event = c("fail",
"fail", "fail", "fail", "pass", "fail", "fail", "pass", "fail",
"fail", "pass", "pass", "pass", "fail", "fail", "pass", "pass",
"fail", "pass", "pass", "pass", "pass", "pass", "pass", "fail",
"fail", "pass", "pass", "fail", "pass", "pass", "pass", "pass",
"pass", "fail", "pass", "fail", "fail", "fail", "pass", "pass",
"pass", "fail", "pass", "pass", "fail", "pass", "fail", "fail",
"pass", "fail", "fail", "pass", "fail", "pass", "fail", "pass",
"fail", "fail", "fail", "fail", "pass", "pass", "fail", "pass",
"pass", "fail", "pass", "fail", "pass", "pass", "fail", "pass",
"fail", "fail", "pass", "pass", "fail", "pass", "pass", "fail",
"pass", "fail", "pass", "fail", "pass", "pass", "pass", "pass",
"fail", "pass", "pass", "fail", "pass", "fail", "pass", "fail",
"pass", "pass", "fail", "pass", "pass", "fail", "pass", "pass",
"fail", "pass", "fail", "fail", "fail", "pass", "pass", "pass",
"fail", "fail", "fail", "fail", "fail", "fail", "fail", "fail",
"fail", "pass", "fail", "fail", "fail", "pass", "pass", "pass",
"pass", "fail", "pass", "pass", "fail", "fail", "pass", "pass",
"fail", "fail", "fail")), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -140L))

>head(df)
file_number reader event
3098129 aa fail
3096451 aa fail
3096774 aa fail
3095276 aa fail

但是当我运行以下 tidyr::pivot_wider 时,我得到了输出 <S3: vctrs_list_of> 。我认为这与 names_from 列中有多个相同类型的值有关。
df %>%
tidyr::pivot_wider(id_cols = file_number, names_from = reader, values_from = event)

id aa ae
3098129 <S3: vctrs_list_of> <S3: vctrs_list_of>
3096451 <S3: vctrs_list_of> <S3: vctrs_list_of>

连同以下警告:
Values in `event` are not uniquely identified; output will contain list-cols.
* Use `values_fn = list(event = list)` to suppress this warning.
* Use `values_fn = list(event = length)` to identify where the duplicates arise
* Use `values_fn = list(event = summary_fun)` to summarise duplicates

我的问题是: 为什么 pivot_wider 输出 S3 向量列表?

编辑
-添加了更好的可重现示例。
-重新定义的问题。

最佳答案

一般来说,如果我们有 names_from没有重复行的序列标识符的列,这可能发生

library(tidyr)
library(dplyr)
df %>%
pivot_wider(names_from = reader, values_from = event)
# A tibble: 124 x 8
# file_number aa ae db dl mk mm np
# <chr> <list<chr>> <list<chr>> <list<chr>> <list<chr>> <list<chr>> <list<chr>> <list<chr>>
# 1 3098129 [1] [0] [0] [0] [0] [0] [0]
# 2 3096451 [1] [0] [0] [0] [0] [0] [0]
# 3 3096774 [1] [0] [0] [0] [0] [0] [1]
# 4 3095276 [1] [0] [0] [0] [0] [0] [0]
# 5 3095464 [1] [0] [0] [0] [0] [0] [0]
# 6 3096846 [1] [0] [0] [0] [0] [0] [0]
# 7 3097132 [1] [0] [0] [0] [0] [0] [0]
# 8 3096355 [1] [0] [0] [0] [0] [0] [0]
# 9 3096951 [1] [0] [0] [1] [0] [0] [0]
#10 3096328 [1] [0] [0] [0] [0] [0] [0]
# … with 114 more rows

因此,对于这些情况,我们需要通过分组变量创建序列
df %>%        
group_by(reader) %>%
mutate(rn = row_number()) %>% # recreated unique identifier column
pivot_wider(names_from = reader, values_from = event)
# A tibble: 139 x 9
# file_number rn aa ae db dl mk mm np
# <chr> <int> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
# 1 3098129 1 fail <NA> <NA> <NA> <NA> <NA> <NA>
# 2 3096451 2 fail <NA> <NA> <NA> <NA> <NA> <NA>
# 3 3096774 3 fail <NA> <NA> <NA> <NA> <NA> <NA>
# 4 3095276 4 fail <NA> <NA> <NA> <NA> <NA> <NA>
# 5 3095464 5 pass <NA> <NA> <NA> <NA> <NA> <NA>
# 6 3096846 6 fail <NA> <NA> <NA> <NA> <NA> <NA>
# 7 3097132 7 fail <NA> <NA> <NA> <NA> <NA> <NA>
# 8 3096355 8 pass <NA> <NA> <NA> <NA> <NA> <NA>
# 9 3096951 9 fail <NA> <NA> <NA> <NA> <NA> <NA>
#10 3096328 10 fail <NA> <NA> <NA> <NA> <NA> <NA>
# … with 129 more rows

列都是 factors因为在 data.frame打电话,如果我们不。指定 stringsAsFactors = FALSE , 默认情况下是 TRUE
str(df)
#'data.frame': 10 obs. of 3 variables:
# $ id : Factor w/ 5 levels "1","2","3","4",..: 1 2 3 4 5 1 2 3 4 5
# $ reader: Factor w/ 2 levels "aa","bb": 1 1 1 1 1 2 2 2 2 2
# $ event : Factor w/ 2 levels "0","1": 2 2 1 1 1 2 1 2 1 2

相反,指定 stringsAsFactors = FALSE列将是 character
df <- data.frame(id = as.character(rep(seq(1:5),2)), 
reader = c("aa","aa","aa","aa","aa","bb","bb","bb","bb","bb"),
event = as.character(rbinom(10, size = 1, prob=0.5)),
stringsAsFactors = FALSE
)

关于r - pivot_wider 输出 <S3 : vctrs_list_of> objects,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58035452/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com