在 R 中使用 rep() 时出现问题。无效的 "times" 参数

Problem using rep() in R. Invalid "times" argument

我在论坛中寻找解决方案,但没有找到。

我正在使用一个鱼类数据库,我正在尝试从这个 (MRE) 中 运行sform 我的数据框:

 df_initial <- structure(list(year = c(2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 
2011L), haul = c(11L, 11L, 11L, 11L, 11L, 11L, 11L), species = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L), .Label = "Merluccius merluccius", class = "factor"), 
    length = c(29L, 33L, 34L, 37L, 10L, 11L, 12L), number = c(2L, 
    1L, 1L, 1L, 7L, 4L, 5L)), class = "data.frame", row.names = c(NA, 
-7L))

至此

  df_final <-structure(list(year = c(2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 
2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 
2011L, 2011L, 2011L, 2011L, 2011L, 2011L), haul = c(11L, 11L, 
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L, 11L), species = structure(c(1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L), .Label = "Merluccius merluccius", class = "factor"), 
    length = c(29L, 29L, 33L, 34L, 37L, 10L, 10L, 10L, 10L, 10L, 
    10L, 10L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L), number = c(2L, 
    2L, 1L, 1L, 1L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 4L, 4L, 4L, 4L, 
    5L, 5L, 5L, 5L, 5L)), class = "data.frame", row.names = c(NA, 
-21L))

也就是说,我想通过它的数量复制长度大小并保留所有列。

我尝试了几种使用函数 rep() 的方法,但我总是得到同样的错误:invalid 'times' argument。我也试过使用数据类型但没有成功。

我做错了什么?

这是我最后的代码运行

df_final <- df_initial[rep(row.names(df_initial), df_initial$number), 1:5] 

我们非常欢迎任何帮助。提前致谢。

错误很可能是由 number 中的 NA 值引起的。您必须首先处理这些问题,要么删除它们,要么如果您想将它们保留在输出中,请将 NA 替换为某个值。以下是如何同时使用 base R 或 {tidyr}。

删除包含 NAs

的行

基数 R:

# add NA values to example
df_initial$number[5:6] <- NA_integer_

df_cleaned <- df_initial[!is.na(df_initial$number), ]
df_final <- df_cleaned[rep(row.names(df_cleaned), df_cleaned$number), 1:5]

df_final

#>     year haul               species length number
#> 1   2011   11 Merluccius merluccius     29      2
#> 1.1 2011   11 Merluccius merluccius     29      2
#> 2   2011   11 Merluccius merluccius     33      1
#> 3   2011   11 Merluccius merluccius     34      1
#> 4   2011   11 Merluccius merluccius     37      1
#> 7   2011   11 Merluccius merluccius     12      5
#> 7.1 2011   11 Merluccius merluccius     12      5
#> 7.2 2011   11 Merluccius merluccius     12      5
#> 7.3 2011   11 Merluccius merluccius     12      5
#> 7.4 2011   11 Merluccius merluccius     12      5

整理师:

library(tidyr)

df_final <- df_initial %>% 
  drop_na(number) %>% 
  uncount(weights = number, .remove = FALSE)

df_final

#>    year haul               species length number
#> 1  2011   11 Merluccius merluccius     29      2
#> 2  2011   11 Merluccius merluccius     29      2
#> 3  2011   11 Merluccius merluccius     33      1
#> 4  2011   11 Merluccius merluccius     34      1
#> 5  2011   11 Merluccius merluccius     37      1
#> 6  2011   11 Merluccius merluccius     12      5
#> 7  2011   11 Merluccius merluccius     12      5
#> 8  2011   11 Merluccius merluccius     12      5
#> 9  2011   11 Merluccius merluccius     12      5
#> 10 2011   11 Merluccius merluccius     12      5

替换NAs

基数 R:

df_cleaned <- df_initial
df_cleaned$number[is.na(df_initial$number)] <- 1L
df_final <- df_cleaned[rep(row.names(df_cleaned), df_cleaned$number), 1:5]

df_final

#>     year haul               species length number
#> 1   2011   11 Merluccius merluccius     29      2
#> 1.1 2011   11 Merluccius merluccius     29      2
#> 2   2011   11 Merluccius merluccius     33      1
#> 3   2011   11 Merluccius merluccius     34      1
#> 4   2011   11 Merluccius merluccius     37      1
#> 5   2011   11 Merluccius merluccius     10      1
#> 6   2011   11 Merluccius merluccius     11      1
#> 7   2011   11 Merluccius merluccius     12      5
#> 7.1 2011   11 Merluccius merluccius     12      5
#> 7.2 2011   11 Merluccius merluccius     12      5
#> 7.3 2011   11 Merluccius merluccius     12      5
#> 7.4 2011   11 Merluccius merluccius     12      5

tidyr

df_final <- df_initial %>% 
  replace_na(list(number = 1L)) %>% 
  uncount(weights = number, .remove = FALSE)
df_final

#>    year haul               species length number
#> 1  2011   11 Merluccius merluccius     29      2
#> 2  2011   11 Merluccius merluccius     29      2
#> 3  2011   11 Merluccius merluccius     33      1
#> 4  2011   11 Merluccius merluccius     34      1
#> 5  2011   11 Merluccius merluccius     37      1
#> 6  2011   11 Merluccius merluccius     10      1
#> 7  2011   11 Merluccius merluccius     11      1
#> 8  2011   11 Merluccius merluccius     12      5
#> 9  2011   11 Merluccius merluccius     12      5
#> 10 2011   11 Merluccius merluccius     12      5
#> 11 2011   11 Merluccius merluccius     12      5
#> 12 2011   11 Merluccius merluccius     12      5

reprex package (v2.0.1)

于 2022-03-15 创建