为什么在 R 中使用 left_join() 时会出现此"invalid 'times' argument"错误?

  • 本文关键字:invalid argument times 错误 join left r
  • 更新时间 :
  • 英文 :


我有两个tibble,我不明白为什么当我使用left_join合并它们时会出现错误:

df <- structure(list(animal = c("cat", "dog", "mouse", "rat", 
"pidgeon", "fish"), value = c(-2.71, -2.63, 
-2.66, -6.99, -2.11, -6.44), ID = c("2700", 
NA, NA, "4821", "55117", "NA")), row.names = c(NA, -5L
), class = c("tbl_df", "tbl", "data.frame"))
> df
# A tibble: 5 x 3
animal  value ID   
<chr>   <dbl> <chr>
1 cat     -2.71 2700 
2 dog     -2.63 NA   
3 mouse   -2.66 NA   
4 rat     -6.99 4821 
5 pidgeon -2.11 55117
new_vals <- structure(list(animal = c("dog", "pidgeon"), ID_new = c("123456", "25255")), row.names = c(NA, -2L), class = c("tbl_df", 
"tbl", "data.frame"))
> new_vals
# A tibble: 2 x 2
animal  ID_new
<chr>   <chr> 
1 dog     123456
2 pidgeon 25255 

df_new <- left_join(
df,
new_vals,
by = "animal"
)
Error in rep(x_loc, lengths(y_loc)) : invalid 'times' argument

有人能告诉我错误在哪里吗?我感觉自己快要疯了!

您的df在某种程度上已损坏。

如果查看您提供的structure(.)输出,您会发现"animal"的长度为6,但该结构认为row.names = c(NA, -5L)只有五行。当您查看df时,它确实只有5行显示,但仍有"fish"要显示。

使用以编程方式修复它

attr(df, "row.names") <- seq_along(df$animal)
df
# # A tibble: 6 x 3
#   animal  value ID   
# * <chr>   <dbl> <chr>
# 1 cat     -2.71 2700 
# 2 dog     -2.63 <NA> 
# 3 mouse   -2.66 <NA> 
# 4 rat     -6.99 4821 
# 5 pidgeon -2.11 55117
# 6 fish    -6.44 NA   

现在显示六行,连接现在可以工作了。

left_join(df, new_vals, by = "animal")
# # A tibble: 6 x 4
#   animal  value ID    ID_new
#   <chr>   <dbl> <chr> <chr> 
# 1 cat     -2.71 2700  <NA>  
# 2 dog     -2.63 <NA>  123456
# 3 mouse   -2.66 <NA>  <NA>  
# 4 rat     -6.99 4821  <NA>  
# 5 pidgeon -2.11 55117 25255 
# 6 fish    -6.44 NA    <NA>  

(我不知道这是怎么发生的……如果你在其他地方以编程的方式读到这篇文章,你应该检查代码在做什么,要么停止做你做的事情,要么报告一个错误……这永远不应该发生。(

由于某些原因,在打印时,由于某些损坏,最后一行未包含在数据集中。一种选择是在unclass激活后重新启用它

left_join(as_tibble(unclass(df)), new_vals, by = 'animal')

-输出

# A tibble: 6 x 4
#  animal  value ID    ID_new
#  <chr>   <dbl> <chr> <chr> 
#1 cat     -2.71 2700  <NA>  
#2 dog     -2.63 <NA>  123456
#3 mouse   -2.66 <NA>  <NA>  
#4 rat     -6.99 4821  <NA>  
#5 pidgeon -2.11 55117 25255 
#6 fish    -6.44 NA    <NA> 

如果某些转换更改属性,则可能发生这种情况

df1 <- as_tibble(unclass(df))
df1
# A tibble: 6 x 3
#  animal  value ID   
#  <chr>   <dbl> <chr>
#1 cat     -2.71 2700 
#2 dog     -2.63 <NA> 
#3 mouse   -2.66 <NA> 
#4 rat     -6.99 4821 
#5 pidgeon -2.11 55117
#6 fish    -6.44 NA  

现在,我们可以修改attributes,因为这是list,而list元素可以具有不同的length

attributes(df1)$row.names <- 1:5

但是,我们不能进行

row.names(df1) <- 1:5

现在我们得到了损坏的数据集

df1
# A tibble: 5 x 3
#  animal  value ID   
#* <chr>   <dbl> <chr>
#1 cat     -2.71 2700 
#2 dog     -2.63 <NA> 
#3 mouse   -2.66 <NA> 
#4 rat     -6.99 4821 
#5 pidgeon -2.11 55117


CCD_ 13移除CCD_ 14属性并返回具有CCD_ 16属性的CCD_。现在,我们可以找到每个list元素的lengthrow.nameslength的差异

unclass(df1)
#$animal
#[1] "cat"     "dog"     "mouse"   "rat"     "pidgeon" "fish"   
#$value
#[1] -2.71 -2.63 -2.66 -6.99 -2.11 -6.44
#$ID
#[1] "2700"  NA      NA      "4821"  "55117" "NA"   
#attr(,"row.names")
#[1] 1 2 3 4 5

相关内容

最新更新