r - 根据另一个 df 替换选定行中的多个列值
r - replacing multiple column values in selected rows based on another df
我正在寻找一种迭代方式(例如 for loop
、purrr
)用另一个数据帧中类似列的值替换数据帧选定行的某些列的值。
# Here is a toy df
df <- dplyr::tibble(
"id" = c("id_a", "id_b", "id_c"),
"subj_1" = c("subj_1_a", "subj_1_b", "subj_1_c"),
"subj_2" = c("subj_2_a", "subj_2_b","subj_2_c"),
"subj_3" = c("subj_3_a", "subj_3_b","subj_3_c")
)
# id subj_1 subj_2 subj_3
# <chr> <chr> <chr> <chr>
# 1 id_a subj_1_a subj_2_a subj_3_a
# 2 id_b subj_1_b subj_2_b subj_3_b
# 3 id_c subj_1_c subj_2_c subj_3_c
这是一个数据名,其中包含我想在列中替换的值:
df_replace <- dplyr::tibble(
"id" = "id_a",
"subj_1" = "subj_1_a_REP",
"subj_2" = "subj_2__REP",
"subj_3" = "subj_2__REP"
)
这是我求的结果(一一得到):
df2 <- df
df2$subj_1[df2$id == "id_a"] <- df_replace$subj_1
df2$subj_2[df2$id == "id_a"] <- df_replace$subj_2
df2$subj_3[df2$id == "id_a"] <- df_replace$subj_3
# id subj_1 subj_2 subj_3
# <chr> <chr> <chr> <chr>
# 1 id_a subj_1_a_REP subj_2__REP subj_2__REP
# 2 id_b subj_1_b subj_2_b subj_3_b
# 3 id_c subj_1_c subj_2_c subj_3_c
如何获得与 loop
或 purrr
相同的迭代?
我的尝试无效 - 我认为它存在一些索引问题
col_subj_names <- names(df_replace)[-1] # without id
# pick the id
i <- "id_a"
# for loop (NOT WORKING!)
for (j in seq_along(col_subj_names)) {
col <- col_subj_names[j]
df %>%
dplyr::filter(id == i) %>%
dplyr::mutate(., col = df_replace$col)
df3 <- df
}
我也看不懂警告信息:
Unknown or uninitialized column:
col.
这里根本不需要循环。您可以一次性替换整行。
df[df$id %in% df_replace$id,] <- df_replace
df
#> # A tibble: 3 x 4
#> id subj_1 subj_2 subj_3
#> <chr> <chr> <chr> <chr>
#> 1 id_a subj_1_a_REP subj_2__REP subj_2__REP
#> 2 id_b subj_1_b subj_2_b subj_3_b
#> 3 id_c subj_1_c subj_2_c subj_3_c
请注意,即使您有多个 id_a
行,这仍然有效 - 它们都将被适当地替换为单行代码。
尝试 rows_update(df_replace)
:
library(tidyverse)
df_replace <- tibble(
"id" = "id_a",
"subj_1" = "subj_1_a_REP",
"subj_2" = "subj_2__REP",
"subj_3" = "subj_2__REP"
)
tibble(
"id" = c("id_a", "id_b", "id_c"),
"subj_1" = c("subj_1_a", "subj_1_b", "subj_1_c"),
"subj_2" = c("subj_2_a", "subj_2_b","subj_2_c"),
"subj_3" = c("subj_3_a", "subj_3_b","subj_3_c")
) |>
rows_update(df_replace)
#> Matching, by = "id"
#> # A tibble: 3 × 4
#> id subj_1 subj_2 subj_3
#> <chr> <chr> <chr> <chr>
#> 1 id_a subj_1_a_REP subj_2__REP subj_2__REP
#> 2 id_b subj_1_b subj_2_b subj_3_b
#> 3 id_c subj_1_c subj_2_c subj_3_c
由 reprex package (v2.0.1)
于 2022-05-17 创建
我正在寻找一种迭代方式(例如 for loop
、purrr
)用另一个数据帧中类似列的值替换数据帧选定行的某些列的值。
# Here is a toy df
df <- dplyr::tibble(
"id" = c("id_a", "id_b", "id_c"),
"subj_1" = c("subj_1_a", "subj_1_b", "subj_1_c"),
"subj_2" = c("subj_2_a", "subj_2_b","subj_2_c"),
"subj_3" = c("subj_3_a", "subj_3_b","subj_3_c")
)
# id subj_1 subj_2 subj_3
# <chr> <chr> <chr> <chr>
# 1 id_a subj_1_a subj_2_a subj_3_a
# 2 id_b subj_1_b subj_2_b subj_3_b
# 3 id_c subj_1_c subj_2_c subj_3_c
这是一个数据名,其中包含我想在列中替换的值:
df_replace <- dplyr::tibble(
"id" = "id_a",
"subj_1" = "subj_1_a_REP",
"subj_2" = "subj_2__REP",
"subj_3" = "subj_2__REP"
)
这是我求的结果(一一得到):
df2 <- df
df2$subj_1[df2$id == "id_a"] <- df_replace$subj_1
df2$subj_2[df2$id == "id_a"] <- df_replace$subj_2
df2$subj_3[df2$id == "id_a"] <- df_replace$subj_3
# id subj_1 subj_2 subj_3
# <chr> <chr> <chr> <chr>
# 1 id_a subj_1_a_REP subj_2__REP subj_2__REP
# 2 id_b subj_1_b subj_2_b subj_3_b
# 3 id_c subj_1_c subj_2_c subj_3_c
如何获得与 loop
或 purrr
相同的迭代?
我的尝试无效 - 我认为它存在一些索引问题
col_subj_names <- names(df_replace)[-1] # without id
# pick the id
i <- "id_a"
# for loop (NOT WORKING!)
for (j in seq_along(col_subj_names)) {
col <- col_subj_names[j]
df %>%
dplyr::filter(id == i) %>%
dplyr::mutate(., col = df_replace$col)
df3 <- df
}
我也看不懂警告信息:
Unknown or uninitialized column:
col.
这里根本不需要循环。您可以一次性替换整行。
df[df$id %in% df_replace$id,] <- df_replace
df
#> # A tibble: 3 x 4
#> id subj_1 subj_2 subj_3
#> <chr> <chr> <chr> <chr>
#> 1 id_a subj_1_a_REP subj_2__REP subj_2__REP
#> 2 id_b subj_1_b subj_2_b subj_3_b
#> 3 id_c subj_1_c subj_2_c subj_3_c
请注意,即使您有多个 id_a
行,这仍然有效 - 它们都将被适当地替换为单行代码。
尝试 rows_update(df_replace)
:
library(tidyverse)
df_replace <- tibble(
"id" = "id_a",
"subj_1" = "subj_1_a_REP",
"subj_2" = "subj_2__REP",
"subj_3" = "subj_2__REP"
)
tibble(
"id" = c("id_a", "id_b", "id_c"),
"subj_1" = c("subj_1_a", "subj_1_b", "subj_1_c"),
"subj_2" = c("subj_2_a", "subj_2_b","subj_2_c"),
"subj_3" = c("subj_3_a", "subj_3_b","subj_3_c")
) |>
rows_update(df_replace)
#> Matching, by = "id"
#> # A tibble: 3 × 4
#> id subj_1 subj_2 subj_3
#> <chr> <chr> <chr> <chr>
#> 1 id_a subj_1_a_REP subj_2__REP subj_2__REP
#> 2 id_b subj_1_b subj_2_b subj_3_b
#> 3 id_c subj_1_c subj_2_c subj_3_c
由 reprex package (v2.0.1)
于 2022-05-17 创建