I´ve got some sample data
data1 = data.frame(name = c("cat", "dog", "parrot"), freq = c(1,2,3))
data2 = data.frame(name = c("Cat", "snake", "Dog", freq2 = c(2,3,4)))
data1$name = as.character(data1$name)
data2$name = as.character(data2$name)
which I want to join, but e.g. "cat" and "Cat" should be treated as the same value. I thought of using tolower
and first to determine the entries which appear in both data frames by
in_both = data1[(tolower(data1$name) %in% tolower(data2$name)),]
Then I want to join with data2
, but that doesn't work because the names doesn't match.
library(dplyr)
left_join(in_both, data2)
Is there a way to join by using tolower
?
If you don't want to alter your original data2
, as @AshofFire suggested, you can decapitalize the values in name
in a pipe %>%
and then perform the join operation:
data2 %>%
mutate(name = str_to_lower(name)) %>%
inner_join(data1, by = "name")
name freq2 freq
1 cat 2 1
2 dog 4 2