-1

I have 2 dataframes, (people_on_bikes2) one of them has the info of the Data and Districts with number of people. (f2) The second one has the information of the localization of the Districts. I need to join both of them by a specific column of the second one.

people_on_bikes2:

people_on_bikes2

f2:

f2

I need to join by the column nom_comptage, but I dont know how to do it if in the first dataframe I dont have the same names of columns

A. Suliman
  • 12,923
  • 5
  • 24
  • 37
Alexa
  • 99
  • 1
  • 6
  • 2
    [See here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on making an R question that folks can help with. That includes a sample of data, all necessary code, and a clear explanation of what you're trying to do and what hasn't worked. Merging data frames is covered in lots of SO posts; which ones have you looked at, and how have they not already helped? – camille Aug 18 '19 at 18:10
  • 1
    Meanwhile, check [this](https://stackoverflow.com/questions/52667837/tidyverse-gather-with-rowdata-from-other-data-frame) question. – A. Suliman Aug 18 '19 at 18:11

1 Answers1

0

I would suggest unpivoting the first data frame with function melt from library(reshape2), and then joining by the column you intend to.

tmp1 <- data.frame(ex1 = c(1,2,3), ex2 = c(30,20,30), cities = c('A', 'B', 'C'))
tmp2 <- data.frame(dates = c('jan','feb','mar'), A = c('Aval1', 'Aval2', 'Aval3'),
                   B = c('Bval1', 'Bval2', 'Bval3'),
                   C = c('Cval1', 'Cval2', 'Cval3'))

library(reshape2)
tmp3 <- melt(tmp2,  id = "dates", measured = c('A', 'B', 'C'))

names(tmp3)[2] <- "cities"
library(dplyr)
inner_join(tmp1, tmp3)
Patryk Kowalski
  • 561
  • 3
  • 13