我在論壇中尋找解決方案,但我沒有得到任何解決方案。
我正在使用一個魚資料庫,我正在嘗試從這個(MRE)轉換我的資料框:
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]
任何幫助都將受到歡迎。提前致謝。
uj5u.com熱心網友回復:
該錯誤很可能是由中的NA值引起的number。您必須首先處理這些,或者通過洗掉它們,或者如果您想將它們保留在輸出中,則替換NA為一些值。以下是如何同時使用基礎 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
替換NA_
基數 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
蒂迪爾
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 包于 2022-03-15 創建(v2.0.1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/444470.html
上一篇:根據列匹配回傳其他行中存在的值
