My data:
data1 <- data.frame(from = c(1, 2, 13, 4),
to = c(4, 3, 9, 1),
values = c(12, 56, 67, 78))
data2 <- data.frame(place = c("NY", "London", "Brest", "Nantes"),
id = c(1, 2, 3, 4))
My results:
from to values
1 4 12
2 3 56
13 9 67
4 1 78
place id
NY 1
London 2
Brest 3
Nantes 4
What I expect using join function from dplyr package (in a new table)
from to values
NY Nantes 12
London Brest 56
London NY 78
What I tried:
data3<- inner_join (data1, data2, by =c("from" = "id", "to" = "id"))
data3
some references:
https://stat545-ubc.github.io/bit001_dplyr-cheatsheet.html
https://cran.r-project.org/web/packages/dplyr/vignettes/two-table.html
A bigger example with mix data
Consider that i have 50 columns with geographical data ("places") and non geographical data (levels, values)
I doesnt wish to change the columns'order of my d.f.
I want to keep my columns names
data1 <- data.frame(levels1 = c("name1", "name2", "name3", "name4"),
value1 = c(4, 3, 9, 1),
firstplace = c(1, 2, 13, 4),
secondplace = c(1, 2, 2, 4),
value2 = c(78, 3000, 90, 101),
thirdplace =c(1, 1, 2, 4),
fourthplace=c(4, 4, 4, 4),
fifthplace=c(1, 2, 3, 4),
value3 = c(12, 56, 67, 78))
data2 <- data.frame(place = c("NY", "London", "Brest", "Nantes"),
id = c(1, 2, 3, 4))
A example with different names (more complex?)
I doesnt wish to change the columns'order of my d.f.
I want to keep my columns names
data1 <- data.frame(levels1 = c("name1", "name2", "name3", "name4"),
value1 = c(4, 3, 9, 1),
shops= c(1, 2, 13, 4),
after_sales_service = c(1, 2, 2, 4),
value2 = c(78, 3000, 90, 101),
provider =c(1, 1, 2, 4),
seller=c(4, 4, 4, 4),
maker=c(1, 2, 3, 4),
value3 = c(12, 56, 67, 78))
data2 <- data.frame(place = c("NY", "London", "Brest", "Nantes"),
id = c(1, 2, 3, 4))