简体   繁体   English

如何用另一列中的值替换不同列中的值? (右)

[英]How to replace values in differents columns with values in another column? (R)

library(tidyverse)图书馆(tidyverse)

reprex for you to reproduce: reprex 让您重现:

library(tidyverse)

tibble(
  x1 = c(1, 2, NA, NA, 5),
  y1 = c(4, 3, NA, NA, 7),
  x2 = c(NA, NA, 6, 7, NA),
  y2 = c(NA, NA, 2, 4, NA),
  replace1 = c("A", "B", "C", "D", "E"),
  replace2 = c("F", "G", "H", "I", "J")
)

I have this dataframe:我有这个 dataframe:

# A tibble: 5 x 6
     x1    y1    x2    y2 replace1 replace2
  <dbl> <dbl> <dbl> <dbl> <chr>    <chr>   
1     1     4    NA    NA A        F       
2     2     3    NA    NA B        G       
3    NA    NA     6     2 C        H       
4    NA    NA     7     4 D        I       
5     5     7    NA    NA E        J

I need the dataframe to be like this, which tidyverse pipeline will get me that?.我需要 dataframe 是这样的,哪个 tidyverse 管道可以让我做到这一点?

# A tibble: 5 x 6
  x1    y1    x2    y2    replace1 replace2
  <chr> <chr> <chr> <chr> <chr>    <chr>   
1 1     4     A     F     A        F       
2 2     3     B     G     B        G       
3 C     H     6     2     C        H       
4 D     I     7     4     D        I       
5 5     7     E     J     E        J 

We can use我们可以用

library(dplyr)
library(stringr)
df1 %>% 
     mutate(across(1:4, ~ coalesce(as.character(.), 
        get(str_replace(cur_column(), "\\D+", "replace")))))

-output -输出

# A tibble: 5 x 6
#  x1    y1    x2    y2    replace1 replace2
#  <chr> <chr> <chr> <chr> <chr>    <chr>   
#1 1     4     F     F     A        F       
#2 2     3     G     G     B        G       
#3 C     C     6     2     C        H       
#4 D     D     7     4     D        I       
#5 5     7     J     J     E        J       

Or if it is based on 'x', 'y'或者如果它基于'x','y'

 df1 %>% 
     mutate(replace_x = replace1, replace_y = replace2) %>% 
     mutate(across(1:4, ~ coalesce(as.character(.), 
         get(str_replace(cur_column(), "(\\D+)\\d+", "replace_\\1"))))) %>%   
     select(-matches('replace_[xy]'))
# A tibble: 5 x 6
#  x1    y1    x2    y2    replace1 replace2
#  <chr> <chr> <chr> <chr> <chr>    <chr>   
#1 1     4     A     F     A        F       
#2 2     3     B     G     B        G       
#3 C     H     6     2     C        H       
#4 D     I     7     4     D        I       
#5 5     7     E     J     E        J       

Not tidy but a base R option with apply .不整洁,但一个基本的 R 选项与apply

cols <- grep('replace', names(df))
df[] <- trimws(t(apply(df, 1, function(x) {x[is.na(x)] <- x[cols];x})))

#   x1    y1    x2    y2  replace1 replace2
#  <chr> <chr> <chr> <chr> <chr>    <chr>   
#1 1     4     A     F     A        F       
#2 2     3     B     G     B        G       
#3 C     H     6     2     C        H       
#4 D     I     7     4     D        I       
#5 5     7     E     J     E        J     

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据R中的/另一列替换几列的值? - How to replace values of several columns based on/ another column in R? R:将多列数据帧中的多个值替换为另一列中的值 - R: Replace multiple values in multiple columns of dataframes with values in another column 如何基于R中另一列中的值替换列值? - How to replace column values based on values in another column in R? 如何根据R中另一列中的值替换数据框的列中的值? - How to replace values in the columns of a dataframe based on the values in the other column in R? 如何根据每行中 R 中的/另一列替换几列的值? - How to replace values of several columns based on/ another column in R within each row? 如何根据另一列的值替换 R 中的 NA 值? - How to replace NA values in R depending on the value of another column? 如何将列中的当前数值替换为 R 中的另一个值? - How to replace current numeric values in a column to another value in R? 在 R 中,如何根据条件将列中的值替换为另一个数据集的另一列的值? - In R, how to replace values in a column with values of another column of another data set based on a condition? r-替换列中的值 - r - Replace values in columns 用 R dplyr 中另一列的值替换一列的值 - Replace the values of one column with values of another column in R dplyr
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM