How to Merge Two Columns in R with a Specific Symbol

How to combine multiple character columns into a single column in an R data frame

Try this:

AL_Blocks$BLOCK_ID<- with(AL_Blocks, paste0(STATE, COUNTY, TRACT, BLOCK))

there was a typo in County... it should've been COUNTY. Also, you don't need the collapse parameter.

I hope that helps.

Combining two columns with character strings into a new column

We can use str_c from stringr

library(dplyr)
library(stringr)
df %>%
mutate(Col3 = str_c(Col1, Col2))

Or with unite

library(tidyr)
df %>%
unite(Col3, Col1, Col2, sep="", remove = FALSE)

How to combine two columns using some conditions?

Here is one idea. Use coalesce first to get the rows with only one NA to have the correct sex. And then use an ifelse to change those rows with different sexes to ?.

Notice that if you have a row with both columns are NA, this solution will return NA. Please make sure this is the behavior you want.

library(dplyr)

dat2 <- dat %>%
mutate(Sex = coalesce(.$Sex.x, .$Sex.y)) %>%
mutate(Sex = ifelse(Sex.x != Sex.y & !is.na(Sex.x) & !is.na(Sex.y), "?", Sex))
dat2
# Sex.x Sex.y Sex
# 1 M M M
# 2 F F F
# 3 F M ?
# 4 M M M
# 5 F F F
# 6 M M M
# 7 <NA> M M
# 8 F F F

DATA

dat <- read.table(text = "Sex.x  Sex.y 
M M
F F
F M
M M
F F
M M
NA M
F F", header = TRUE)

Merging two data frames with columns with certain patterns in strings

One possible solution. I am not completely sure what you want to do with the 'x' in the strings, I have kept them in the linkage key, but by changing the \\1\\2 to \\1 you keep only the first letter.

a <- data.frame(
Name = paste0(".", c("tony", "tom", "foo", "bar", "foobar"), ".x.rds"),
Numbers = rnorm(5)
)

b <- data.frame(
Name = paste0(c("tony", "tom", "bar", "foobar", "company"), ".x"),
ChaR = LETTERS[11:15]
)

# String consists of 'point letter1 point letter2 point rds'; replace by
# 'letter1 letter2'
a$Name_stand <- gsub("^\\.([a-z]+)\\.([a-z]+)\\.rds$", "\\1\\2", a$Name)

# String consists of 'letter1 point letter2'; replace by 'letter1 letter2'
b$Name_stand <- gsub("^([a-z]+)\\.([a-z]+)$", "\\1\\2", b$Name)

result <- merge(a, b, all = TRUE, by = "Name_stand")

Output:

#> result
# Name_stand Name.x Numbers Name.y ChaR
#1 barx .bar.x.rds 1.38072696 bar.x M
#2 companyx <NA> NA company.x O
#3 foobarx .foobar.x.rds -1.53076596 foobar.x N
#4 foox .foo.x.rds 1.40829287 <NA> <NA>
#5 tomx .tom.x.rds -0.01204651 tom.x L
#6 tonyx .tony.x.rds 0.34159406 tony.x K

Another, perhaps somewhat more robust (to variations of the strings such as 'tom.rds' and 'tom' which will still be linked; this can of course also be a disadvantage)/

# Remove the rds from a$Name
a$Name_stand <- gsub("rds$" , "", a$Name)
# Remove all non alpha numeric characters from the strings
a$Name_stand <- gsub("[^[:alnum:]]", "", a$Name_stand)
b$Name_stand <- gsub("[^[:alnum:]]", "", b$Name)

result2 <- merge(a, b, all = TRUE, by = "Name_stand")

combine two columns with specific symbol and selective

you can use ifelse

   df$label<-NA
df$label <- ifelse(df$id==1,paste(df$id, "_up_", df$row, sep = ""),df$label)
df$label <- ifelse(df$id==2,paste(df$id, "_down_", df$row, sep = ""),df$label)

How to combine multiple character columns into one columns and remove NA without knowing column numbers

Here is a base R method

input$ALL <- apply(input[-1], 1, function(x) paste(na.omit(x), collapse=" "))
input$ALL
#[1] "tv" "web" "book" "web tv"

join/merge two columns inside a data-frame

df$col3 <- paste(df$col1, df$col2, sep=","). You can also use the sprintf and paste0 functions.

df$col3 <- paste(df$col1, df$col2, sep=",") # comma separator
df$col3 <- paste0(df$col1, df$col2) # for no separator

How to merge two data frames with specific string match in columns in R?

Here's a simple solution:

library(stringr)
library(dplyr)
library(tidyr)
library(magrittr)

data1 %<>% mutate(lname = str_extract(ProfName, "[A-Za-z\\-]+$"))
data2 %<>% mutate(lname = str_extract(ProfName, "^[A-Za-z\\-]+"))

df <- merge(data1, data2, all.y = TRUE, by = "lname")

head(df)

# lname ProfName.x Title Profession # ProfName.y
# 1 Attaway Alan Attaway PhD Professor Attaway, A
# 2 Barr-Pulliam Dereck Barr-Pulliam PhD Assistant Professor Barr-Pulliam, D
# 3 Blandford <NA> <NA> <NA> Blandford, K
# 4 Blum Lisa M. Blum LLM Instructor Blum, L
# 5 Callahan Carolyn M. Callahan PhD Brown-Forman Professor of Accountancy Callahan, C
# 6 Foster Benjamin P. Foster PhD Professor Foster, B


Related Topics



Leave a reply



Submit