繁体   English   中英

为什么在 R 中使用 left_join() 时会出现“无效的 'times' 参数”错误?

[英]Why do I get this “invalid 'times' argument” error when using left_join() in R?

我有两个 tibbles,当我使用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(.) output,您会看到"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 ing 之后重新调用它

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 ,因为这是一个listlist元素可以有不同的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


 

unclass删除data.frame属性并返回带有row.names属性的list 现在,我们可以找到每个list元素的lengthrow.names length的差异

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

暂无
暂无

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

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