use paste
if you dont want lose any data
library(dplyr)
df%>% mutate(Place = paste(Place_English, Place_French),
Plane = paste(Plane_English, Plane_French),
across(Place_English:Plane_French, ~NULL)) ## last line to remove unnecessary columns
or coalesce
if you want to get rid of NA
s
df%>% mutate(Place = coalesce(Place_English, Place_French),
Plane = coalesce(Plane_English, Plane_French),
across(Place_English:Plane_French, ~NULL)) ## last line to remove unnecessary columns
if you want combine more than 2 cols, use unite
from tidyr
. set na.rm
according to your preferences
library(tidyr)
df %>%
unite("Place", colnames(df)[grepl(pattern = "Place", colnames(df))] , remove = T, sep = " ", na.rm = TRUE) %>% ## all cols including "Place" in name
unite("Plane", colnames(df)[grepl(pattern = "Plane", colnames(df))] , remove = T, sep = " ", na.rm = TRUE) ## all cols including "Plane" in name
library(tidyr)
cols_to_paste <- colnames(df[,]) ## to choose only sepecified cols i.e. df[,15:25] or df[,c(15,18,20,25)]
df %>%
unite('Place', cols_to_paste[grepl(pattern = 'Place', cols_to_paste)] , remove = T, sep = " ", na.rm = TRUE) %>% ## all cols including "Place" in name
unite('Plane', cols_to_paste[grepl(pattern = 'Plane', cols_to_paste)] , remove = T, sep = " ", na.rm = TRUE) ## all cols including "Plane" in name