Home > Enterprise >  Connect two datasets with left_join
Connect two datasets with left_join

Time:10-13

Im trying to connect/merge the following datasets, "merged_data" and "GpsData" with function left_join(), but im coming in front of some obstacles.

Code:

> new_obj1<-lapply(merged_data, head)
> dput(head(new_obj1))
list(Country = c(2, 3, 5, 6, 7, 8), `Country Name` = c("England", 
"Costa Rica", "Italy", "India", "Namibia", "Czech Republic"), 
    `Uncertainty Avoidance Societal Practices` = c(4.65123456790123, 
    3.82456140350877, 3.78755868544601, 4.1504854368932, 4.2, 
    4.43958333333333), `Future Orientation Societal Practices` = c(4.27901234567901, 
    3.60350877192982, 3.25258215962441, 4.19271844660194, 3.49333333333333, 
    3.63041666666667), `Power Distance Societal Practices` = c(5.15308641975309, 
    4.73684210526316, 5.4268779342723, 5.46601941747573, 5.29333333333333, 
    3.58875), `Collectivism I Societal Practices (Institutional Collectivism)` = c(4.27160493827161, 
    3.92982456140351, 3.67899061032864, 4.38106796116505, 4.13333333333333, 
    3.60486111111111))


> new_obj2<-lapply(GpsData, head)
> dput(head(new_obj2))
list(country = c("Afghanistan", "Algeria", "Argentina", "Australia", 
"Austria", "Bangladesh"), isocode = c("AFG", "DZA", "ARG", "AUS", 
"AUT", "BGD"), patience = c(-0.201360121369362, 0.0598152466118336, 
-0.229307979345322, 0.65700376033783, 0.608285009860992, 0.0811367109417915
), risktaking = c(0.120764262974262, 0.391530483961105, 0.0415031686425209, 
0.137136548757553, -0.0618291534483433, -0.198067754507065), 
    posrecip = c(0.2896409034729, -0.598255336284637, 0.159679308533669, 
    0.069660022854805, 0.161046594381332, 0.154367566108704), 
    negrecip = c(0.254712462425232, 0.254900813102722, -0.140457272529602, 
    0.0221897512674332, -0.0554154515266418, 0.113288171589375
    ))

I have used this "reference_iso":

> reference_iso <- read.csv("C:/Users/ILIAS/Documents/Dissertation/Data/iso3.csv", encoding = 'UTF-8')[,c(1,3)]
> dput(head(reference_iso))
structure(list(name = c("Afghanistan", "Aland Islands", "Albania", 
"Algeria", "American Samoa", "Andorra"), alpha.3 = c("AFG", "ALA", 
"ALB", "DZA", "ASM", "AND")), row.names = c(NA, 6L), class = "data.frame")

Above i have given you a sample of my datasets, "merged_data" and "GpsData".

I have tried to merge them with function left_join:

all_data <- GpsData %>% 
  left_join(reference_iso, by = c('country' = 'name')) %>% 
  rename(iso3 = 'alpha.3') %>% 
  left_join(merged_data, by = c('iso3' = 'Country Name') )

but as you can see this process doesnt work correctly.

Any advice ?

EDITED

CodePudding user response:

You need to recode you data variables and try to use data.frame

the list is not good for datas joining

CodePudding user response:

As per your sample data, new_obj1 & GpsData are of class list that is why you are not able to use left_join

try converting those lists into dataframes as in code below and try to run your joining code again


df_merged_data <- bind_rows(merged_data)
df_GpsData <- bind_rows(GpsData)

all_data <- df_GpsData %>% 
  left_join(reference_iso, by = c('country' = 'name')) %>% 
  rename(iso3 = 'alpha.3') %>% 
  left_join(df_merged_data, by = c('iso3' = 'Country Name') )
  • Related