這些函數提供了使用第二個數據表修改表中的行的框架。這兩個表與 by
一組關鍵變量相匹配,這些變量的值通常唯一標識每一行。這些函數的靈感來自 SQL 的 INSERT
、 UPDATE
和 DELETE
,並且可以選擇針對選定的後端修改 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
-
給出關鍵列的未命名字符向量。鍵列必須同時存在於
x
和y
中。鍵通常唯一標識每一行,但這僅在使用rows_update()
、rows_patch()
或rows_upsert()
時對y
的鍵值強製執行。默認情況下,我們使用
y
中的第一列,因為第一列是放置標識符變量的合理位置。 - ...
-
傳遞給方法的其他參數。
- conflict
-
對於
rows_insert()
,y
中的鍵與x
中的鍵衝突應該如何處理?如果y
中的鍵已存在於x
中,則會發生衝突。之一:
-
"error"
(默認值)如果y
中的任何鍵與x
中的鍵衝突,將會出錯。 -
"ignore"
將忽略y
中與x
中的鍵衝突的行。
-
- copy
-
如果
x
和y
不是來自同一個數據源,並且copy
是TRUE
,則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 dplyr row_number 整數排名函數
- R dplyr rowwise 按行對輸入進行分組
- R dplyr recode 重新編碼值
- R dplyr rename 重命名列
- R dplyr relocate 更改列順序
- R dplyr reframe 將每個組轉換為任意數量的行
- R dplyr group_trim 修剪分組結構
- R dplyr slice 使用行的位置對行進行子集化
- R dplyr copy_to 將本地數據幀複製到遠程src
- R dplyr sample_n 從表中采樣 n 行
- R dplyr consecutive_id 為連續組合生成唯一標識符
- R dplyr band_members 樂隊成員
- R dplyr mutate-joins 變異連接
- R dplyr nth 從向量中提取第一個、最後一個或第 n 個值
- R dplyr coalesce 找到第一個非缺失元素
- R dplyr group_split 按組分割 DataFrame
- R dplyr mutate 創建、修改和刪除列
- R dplyr order_by 用於排序窗口函數輸出的輔助函數
- R dplyr context 有關“當前”組或變量的信息
- R dplyr percent_rank 比例排名函數
- R dplyr starwars 星球大戰人物
- R dplyr desc 降序
- R dplyr between 檢測值落在指定範圍內的位置
- R dplyr cumall 任何、全部和平均值的累積版本
- R dplyr group_map 對每個組應用一個函數
注:本文由純淨天空篩選整理自Hadley Wickham等大神的英文原創作品 Manipulate individual rows。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。