當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


R dplyr rows 操作單獨的行

這些函數提供了使用第二個數據表修改表中的行的框架。這兩個表與 by 一組關鍵變量相匹配,這些變量的值通常唯一標識每一行。這些函數的靈感來自 SQL 的 INSERTUPDATEDELETE ,並且可以選擇針對選定的後端修改 in_place

  • rows_insert() 添加新行(如 INSERT )。默認情況下, y 中的鍵值不能存在於 x 中。

  • rows_append() 的工作方式與 rows_insert() 類似,但忽略鍵。

  • rows_update() 修改現有行(如 UPDATE )。 y 中的鍵值必須是唯一的,並且默認情況下,y 中的鍵值必須存在於 x 中。

  • rows_patch() 的工作方式與 rows_update() 類似,但僅覆蓋 NA 值。

  • rows_upsert() 根據 y 中的鍵值是否已存在於 x 中進行插入或更新。 y 中的鍵值必須是唯一的。

  • rows_delete() 刪除行(如 DELETE )。默認情況下, y 中的鍵值必須存在於 x 中。

用法

rows_insert(
  x,
  y,
  by = NULL,
  ...,
  conflict = c("error", "ignore"),
  copy = FALSE,
  in_place = FALSE
)

rows_append(x, y, ..., copy = FALSE, in_place = FALSE)

rows_update(
  x,
  y,
  by = NULL,
  ...,
  unmatched = c("error", "ignore"),
  copy = FALSE,
  in_place = FALSE
)

rows_patch(
  x,
  y,
  by = NULL,
  ...,
  unmatched = c("error", "ignore"),
  copy = FALSE,
  in_place = FALSE
)

rows_upsert(x, y, by = NULL, ..., copy = FALSE, in_place = FALSE)

rows_delete(
  x,
  y,
  by = NULL,
  ...,
  unmatched = c("error", "ignore"),
  copy = FALSE,
  in_place = FALSE
)

參數

x, y

一對數據幀或數據幀擴展(例如 tibble)。 y 必須與 x 具有相同的列或子集。

by

給出關鍵列的未命名字符向量。鍵列必須同時存在於 xy 中。鍵通常唯一標識每一行,但這僅在使用 rows_update()rows_patch()rows_upsert() 時對 y 的鍵值強製執行。

默認情況下,我們使用 y 中的第一列,因為第一列是放置標識符變量的合理位置。

...

傳遞給方法的其他參數。

conflict

對於rows_insert()y中的鍵與x中的鍵衝突應該如何處理?如果 y 中的鍵已存在於 x 中,則會發生衝突。

之一:

  • "error" (默認值)如果 y 中的任何鍵與 x 中的鍵衝突,將會出錯。

  • "ignore" 將忽略 y 中與 x 中的鍵衝突的行。

copy

如果 xy 不是來自同一個數據源,並且 copyTRUE ,則 y 將被複製到與 x 相同的源中。這允許您跨 src 連接表,但這是一項潛在昂貴的操作,因此您必須選擇它。

in_place

是否應該就地修改x?此參數僅與可變後端相關(例如數據庫、data.tables)。

TRUE 時,隱形返回 x 的修改版本;當 FALSE 時,返回一個表示結果更改的新對象。

unmatched

對於 rows_update()rows_patch()rows_delete() ,應如何處理 y 中與 x 中的鍵不匹配的鍵?

之一:

  • "error" (默認值)如果 y 中的任何鍵與 x 中的鍵不匹配,則會出錯。

  • "ignore" 將忽略 y 中的行,這些行的鍵與 x 中的鍵不匹配。

x 類型相同的對象。 x的行和列的順序

被盡可能地保留下來。輸出具有以下屬性:

  • rows_update()rows_patch() 保留行數; rows_insert()rows_append()rows_upsert() 返回所有現有行和可能的新行; rows_delete() 返回行的子集。

  • 盡管數據可能會更新,但不會添加、刪除或重新定位列。

  • 組取自x

  • 數據幀屬性取自x

如果是 in_place = TRUE ,結果會以不可見的方式返回。

方法

這些函數是泛型函數,這意味著包可以為其他類提供實現(方法)。有關額外參數和行為差異,請參閱各個方法的文檔。

當前加載的包中可用的方法:

  • rows_insert():dbplyr(tbl_lazy)、dplyr(data.frame)。

  • rows_append():dbplyr(tbl_lazy)、dplyr(data.frame)。

  • rows_update():dbplyr(tbl_lazy)、dplyr(data.frame)。

  • rows_patch():dbplyr(tbl_lazy)、dplyr(data.frame)。

  • rows_upsert():dbplyr(tbl_lazy)、dplyr(data.frame)。

  • rows_delete():dbplyr(tbl_lazy)、dplyr(data.frame)。

例子

data <- tibble(a = 1:3, b = letters[c(1:2, NA)], c = 0.5 + 0:2)
data
#> # A tibble: 3 × 3
#>       a b         c
#>   <int> <chr> <dbl>
#> 1     1 a       0.5
#> 2     2 b       1.5
#> 3     3 NA      2.5

# Insert
rows_insert(data, tibble(a = 4, b = "z"))
#> Matching, by = "a"
#> # A tibble: 4 × 3
#>       a b         c
#>   <int> <chr> <dbl>
#> 1     1 a       0.5
#> 2     2 b       1.5
#> 3     3 NA      2.5
#> 4     4 z      NA  

# By default, if a key in `y` matches a key in `x`, then it can't be inserted
# and will throw an error. Alternatively, you can ignore rows in `y`
# containing keys that conflict with keys in `x` with `conflict = "ignore"`,
# or you can use `rows_append()` to ignore keys entirely.
try(rows_insert(data, tibble(a = 3, b = "z")))
#> Matching, by = "a"
#> Error in rows_insert(data, tibble(a = 3, b = "z")) : 
#>   `y` can't contain keys that already exist in `x`.
#> ℹ The following rows in `y` have keys that already exist in `x`: `c(1)`.
#> ℹ Use `conflict = "ignore"` if you want to ignore these `y` rows.
rows_insert(data, tibble(a = 3, b = "z"), conflict = "ignore")
#> Matching, by = "a"
#> # A tibble: 3 × 3
#>       a b         c
#>   <int> <chr> <dbl>
#> 1     1 a       0.5
#> 2     2 b       1.5
#> 3     3 NA      2.5
rows_append(data, tibble(a = 3, b = "z"))
#> # A tibble: 4 × 3
#>       a b         c
#>   <int> <chr> <dbl>
#> 1     1 a       0.5
#> 2     2 b       1.5
#> 3     3 NA      2.5
#> 4     3 z      NA  

# Update
rows_update(data, tibble(a = 2:3, b = "z"))
#> Matching, by = "a"
#> # A tibble: 3 × 3
#>       a b         c
#>   <int> <chr> <dbl>
#> 1     1 a       0.5
#> 2     2 z       1.5
#> 3     3 z       2.5
rows_update(data, tibble(b = "z", a = 2:3), by = "a")
#> # A tibble: 3 × 3
#>       a b         c
#>   <int> <chr> <dbl>
#> 1     1 a       0.5
#> 2     2 z       1.5
#> 3     3 z       2.5

# Variants: patch and upsert
rows_patch(data, tibble(a = 2:3, b = "z"))
#> Matching, by = "a"
#> # A tibble: 3 × 3
#>       a b         c
#>   <int> <chr> <dbl>
#> 1     1 a       0.5
#> 2     2 b       1.5
#> 3     3 z       2.5
rows_upsert(data, tibble(a = 2:4, b = "z"))
#> Matching, by = "a"
#> # A tibble: 4 × 3
#>       a b         c
#>   <int> <chr> <dbl>
#> 1     1 a       0.5
#> 2     2 z       1.5
#> 3     3 z       2.5
#> 4     4 z      NA  

# Delete and truncate
rows_delete(data, tibble(a = 2:3))
#> Matching, by = "a"
#> # A tibble: 1 × 3
#>       a b         c
#>   <int> <chr> <dbl>
#> 1     1 a       0.5
rows_delete(data, tibble(a = 2:3, b = "b"))
#> Matching, by = "a"
#> Ignoring extra `y` columns: b
#> # A tibble: 1 × 3
#>       a b         c
#>   <int> <chr> <dbl>
#> 1     1 a       0.5

# By default, for update, patch, and delete it is an error if a key in `y`
# doesn't exist in `x`. You can ignore rows in `y` that have unmatched keys
# with `unmatched = "ignore"`.
y <- tibble(a = 3:4, b = "z")
try(rows_update(data, y, by = "a"))
#> Error in rows_update(data, y, by = "a") : 
#>   `y` must contain keys that already exist in `x`.
#> ℹ The following rows in `y` have keys that don't exist in `x`: `c(2)`.
#> ℹ Use `unmatched = "ignore"` if you want to ignore these `y` rows.
rows_update(data, y, by = "a", unmatched = "ignore")
#> # A tibble: 3 × 3
#>       a b         c
#>   <int> <chr> <dbl>
#> 1     1 a       0.5
#> 2     2 b       1.5
#> 3     3 z       2.5
rows_patch(data, y, by = "a", unmatched = "ignore")
#> # A tibble: 3 × 3
#>       a b         c
#>   <int> <chr> <dbl>
#> 1     1 a       0.5
#> 2     2 b       1.5
#> 3     3 z       2.5
rows_delete(data, y, by = "a", unmatched = "ignore")
#> Ignoring extra `y` columns: b
#> # A tibble: 2 × 3
#>       a b         c
#>   <int> <chr> <dbl>
#> 1     1 a       0.5
#> 2     2 b       1.5
源代碼:R/rows.R

相關用法


注:本文由純淨天空篩選整理自Hadley Wickham等大神的英文原創作品 Manipulate individual rows。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。