`group_by()`中的错误:!必须按“.data”中找到的变量进行分组。 ✖ 未找到“数据集”列

问题描述 投票:0回答:1

enter image description here

我正在分析 anscombe 四重奏数据,并且我的所有代码在 R 工作室的 R 项目中运行良好。然而,当我尝试编写 R markdown 文件时,这个错误不断弹出,告诉我“数据集”列不存在。我检查了 colnames() 和数据集确实在那里。我不知道出了什么问题。

在我的 R 项目中,我尝试了 colnames(anscombe_quartet),输出给出了“dataset”、“x”和“y”的列名称,没有任何问题。

r group-by r-markdown tidyverse
1个回答
0
投票

您正在尝试将二进制格式

.rds
文件读取为文本文件,这是不可能发生的。相反,请使用
readRDS

我会尝试重现您的问题。首先,我使用的是

anscombe
,它没有数据中显示的
dataset
列。我假设 your 文件来自不同的数据,但这既不会改变问题,也不会改变解决方案。

data("anscombe", package="datasets")
saveRDS(anscombe, "anscombe_quartet.rds")
gzcon <- gzfile("anscombe_quartet.rds", "rt")
anscombe_quartet <- readLines(gzcon)
# Warning in readLines(gzcon) : line 2 appears to contain an embedded nul
# Warning in readLines(gzcon) : line 4 appears to contain an embedded nul
# Warning in readLines(gzcon) : line 7 appears to contain an embedded nul
# Warning in readLines(gzcon) :
#   line 10 appears to contain an embedded nul
# Warning in readLines(gzcon) :
#   line 14 appears to contain an embedded nul
# Warning in readLines(gzcon) :
#   line 16 appears to contain an embedded nul
# Warning in readLines(gzcon) :
#   line 17 appears to contain an embedded nul
# Warning in readLines(gzcon) :
#   line 18 appears to contain an embedded nul
# Warning in readLines(gzcon) :
#   incomplete final line found on 'anscombe_quartet.rds'
head(anscombe_quartet)
# [1] "X"                                                                                                            
# [2] ""                                                                                                             
# [3] "=p\xa3\xd7"                                                                                                   
# [4] "@%\xae\024z\xe1G\xae@\023G\xae\024z\xe1H@\026\xb8Q\xeb\x85\036\xb8"                                           
# [5] "@\"\x85\036\xb8Q\xeb\x85@ 333333@\030\x85\036\xb8Q\xeb\x85@\b\xcc\xcc\xcc\xcc\xcc\xcd@\"B\x8f\\(\xf5\xc3@\035"
# [6] "=p\xa3\xd7"                                                                                                   

转换为 tibble 掩盖了它不是表格的事实:

as_tibble(anscombe_quartet)
# # A tibble: 18 × 1
#    value                                                                                                                                         
#    <chr>                                                                                                                                         
#  1 "X"                                                                                                                                           
#  2 ""                                                                                                                                            
#  3 "=p\xa3\xd7"                                                                                                                                  
#  4 "@%\xae\x14z\xe1G\xae@\x13G\xae\x14z\xe1H@\x16\xb8Q\xeb\x85\x1e\xb8"                                                                          
#  5 "@\"\x85\x1e\xb8Q\xeb\x85@ 333333@\x18\x85\x1e\xb8Q\xeb\x85@\b\xcc\xcc\xcc\xcc\xcc\xcd@\"B\x8f\\(\xf5\xc3@\x1d"                               
#  6 "=p\xa3\xd7"                                                                                                                                  
#  7 "@\x12\xf5\xc2\x8f\\(\xf6"                                                                                                                    
#  8 "=p\xa3\xd7@\x1b\x14z\xe1G\xae\x14@)z\xe1G\xae\x14{@\x1cp\xa3\xd7"                                                                            
#  9 "=q@\x1f=p\xa3\xd7"                                                                                                                           
# 10 "=@!\xae\x14z\xe1G\xae@\x18Q\xeb\x85\x1e\xb8R@\x15\x8f\\(\xf5\xc2\x8f@ L\xcc\xcc\xcc\xcc\xcd@\x19\xae\x14z\xe1G\xae@\x16\xeb\x85\x1e\xb8Q\xec"
# 11 "=p\xa3\xd7"                                                                                                                                  
# 12 "@\x1e\xd7"                                                                                                                                   
# 13 "=p\xa3\xd7@!\xae\x14z\xe1G\xae@ \xf0\xa3\xd7"                                                                                                
# 14 "=q@\x1c(\xf5\xc2\x8f\\)@\x15"                                                                                                                
# 15 "=@\x1f\xa3\xd7"                                                                                                                              
# 16 "=p\xa4@\x1b\x8f\\(\xf5\xc2\x8f"                                                                                                              
# 17 "data.frame"                                                                                                                                  
# 18 ""                                                                                                                                            

它有一个名为

value
的列,因此任何尝试在原始列上工作的操作都会产生错误。例如,
anscombe
(未修改)有
x1

anscombe |>
  group_by(x4) |>
  summarize(across(everything(), ~ sum(.x)))
# # A tibble: 2 × 8
#      x4    x1    x2    x3    y1    y2    y3    y4
#   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1     8    95    95    95 78.2   79.4 77.1   70.0
# 2    19     4     4     4  4.26   3.1  5.39  12.5

as_tibble(anscombe_quartet) |>
  group_by(x4) |>
  summarize(across(everything(), ~ sum(.x)))
# Error in `group_by()`:
# ! Must group by variables found in `.data`.
# ✖ Column `x4` is not found.
# Run `rlang::last_trace()` to see where the error occurred.

修复方法是使用正确的工具加载数据,

readRDS

anscombe_quartet <- readRDS("anscombe_quartet.rds")
as_tibble(anscombe_quartet) |>
  group_by(x4) |>
  summarize(across(everything(), ~ sum(.x)))
# # A tibble: 2 × 8
#      x4    x1    x2    x3    y1    y2    y3    y4
#   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1     8    95    95    95 78.2   79.4 77.1   70.0
# 2    19     4     4     4  4.26   3.1  5.39  12.5
© www.soinside.com 2019 - 2024. All rights reserved.