if_else two dataframes

Hi,

I have two dataframes 'ucast12' and 'transfer'.

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union


ucast12 <- data.frame(
         OBEC = c(22,33,44,55, 66, 77, 88, 99)

)

transfer <- data.frame(
  MOMC = c(20,33,44,50, 66, 77, 80, 99),
  CISOB = c(22,30,40,50, 60, 70, 80, 90)
  
)

ucast12 %>%
  if_else(OBEC=transfer$MOMC, OBEC=transfer$CISOB,OBEC=OBEC)

#> Error in if_else(., OBEC = transfer$MOMC, OBEC = transfer$CISOB, OBEC = OBEC): unused arguments (OBEC = transfer$MOMC, OBEC = transfer$CISOB, OBEC = OBEC)

I'd like to replace a column 'OBEC' in 'ucast12' by a column 'CISOB' in 'transfer' if OBEC equals MOMC otherwise keep 'OBEC' as it was.

Could you help me with that?

Thank you!

Hello,

Happy to help if you can provide some data or a reprex (see here how to create it: FAQ: How to do a minimal reproducible example ( reprex ) for beginners)

Thank you. Here we go.

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union


ucast12 <- data.frame(
         OBEC = c(22,33,44,55, 66, 77, 88, 99)

)

transfer <- data.frame(
  MOMC = c(20,33,44,50, 66, 77, 80, 99),
  CISOB = c(22,30,40,50, 60, 70, 80, 90)
  
)

ucast12 %>%
  if_else(OBEC=transfer$MOMC, OBEC=transfer$CISOB,OBEC=OBEC)
#> Error in if_else(., OBEC = transfer$MOMC, OBEC = transfer$CISOB, OBEC = OBEC): unused arguments (OBEC = transfer$MOMC, OBEC = transfer$CISOB, OBEC = OBEC)
1 Like

See below. I adjusted your code slightly by merging the tables together (makes for cleaner syntax and you can just select the column you want again relatively easily). I did one mutate to show you it works. The other example show you that you can overwrite that column as well. Let me know if this solves it.

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

ucast12 <- data.frame(
  OBEC = c(22,33,44,55, 66, 77, 88, 99)
  
)

transfer <- data.frame(
  MOMC = c(20,33,44,50, 66, 77, 80, 99),
  CISOB = c(22,30,40,50, 60, 70, 80, 90)
  
)


df <- cbind(ucast12,transfer)

df_final <- df %>% mutate(NEW_OBEC = ifelse(OBEC == MOMC,CISOB,OBEC))
df_final
#>   OBEC MOMC CISOB NEW_OBEC
#> 1   22   20    22       22
#> 2   33   33    30       30
#> 3   44   44    40       40
#> 4   55   50    50       55
#> 5   66   66    60       60
#> 6   77   77    70       70
#> 7   88   80    80       88
#> 8   99   99    90       90

df_final <- df %>% mutate(OBEC = ifelse(OBEC == MOMC,CISOB,OBEC))
df_final
#>   OBEC MOMC CISOB
#> 1   22   20    22
#> 2   30   33    30
#> 3   40   44    40
#> 4   55   50    50
#> 5   60   66    60
#> 6   70   77    70
#> 7   88   80    80
#> 8   90   99    90

Created on 2020-09-23 by the reprex package (v0.3.0)

2 Likes

@Jakub_Komarek, if the above post was the solution just mark it as such :slight_smile:

Hi,

could you help me with the case when

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

ucast12 <- data.frame(
  OBEC = c(21,33,44,51, 66, 77, 81, 99)
  
)

transfer <- data.frame(
  MOMC = c(20,33,44,50, 66, 77, 80, 99),
  CISOB = c(22,30,40,50, 60, 70, 80, 90)
  
)

Thank you!

Is this what you wanted?

library(dplyr)
#> Warning: package 'dplyr' was built under R version 3.6.3
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

ucast12 <- data.frame(
  OBEC = c(21,33,44,51, 66, 77, 81, 99)
  
)

transfer <- data.frame(
  MOMC = c(20,33,44,50, 66, 77, 80, 99),
  CISOB = c(22,30,40,50, 60, 70, 80, 90)
  
)


df <- cbind(ucast12,transfer)

df_final <- df %>% mutate(NEW_OBEC = ifelse(OBEC == MOMC,CISOB,OBEC))
df_final
#>   OBEC MOMC CISOB NEW_OBEC
#> 1   21   20    22       21
#> 2   33   33    30       30
#> 3   44   44    40       40
#> 4   51   50    50       51
#> 5   66   66    60       60
#> 6   77   77    70       70
#> 7   81   80    80       81
#> 8   99   99    90       90

Created on 2020-09-25 by the reprex package (v0.3.0)

Thank you. But I realised I meant the case when

ucast12 <- data.frame(
  OBEC = c(21,33,44,51, 66, 77, 81, 99)
  
)

transfer <- data.frame(
  MOMC = c(20,33,44,50, 66, 77),
  CISOB = c(22,30,40,50, 60, 70)
  
)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.