user36729
user36729

Reputation: 575

Merging two columns of words having small and capital letters in r

I have two data frames and I want to merge them using two columns that are like below:

a <- data.frame(A = c("Ali", "Should Be", "Calif")))
b <- data.frame(B = c("ALI", "CALIF", "SHOULD BE"))

Could you please let me know if it is possible to do it in r?

Upvotes: 1

Views: 1669

Answers (1)

tifu
tifu

Reputation: 1411

One way would be to decapitalize your character values using tolower from base R and then do a merge:

library(dplyr) # for mutating

df1 <- df1 %>%
       mutate(A = tolower(A))

df2 <- df2 %>%
       mutate(B = tolower(B))

df3 <- merge(df1, df2, by.x = "A", by.y = "B")

df3
          A
1       ali
2     calif
3 should be

Is this what you needed?

Edit: The dplyr bit is of course not necessary. If everything is to be done in base R, df1$A=tolower(df1$A) and df2$B=tolower(df2$B) - as suggested in the comments - work just as well.

Upvotes: 1

Related Questions