bring 2 datasets together

Hello,

I 've two datasets :
covid_19:
DateRep : POSIXct, format: "2020-03-23" "2020-03-22" "2020-03-21" "2020-03-20" ... Cases : num 10 0 2 0 0 1 5 6 3 3 ...
Deaths : num 0 0 0 0 0 0 0 0 0 0 ... Countries and territories: chr "Afghanistan" "Afghanistan" "Afghanistan" "Afghanistan" ...
GeoId : chr "AF" "AF" "AF" "AF" ... **continent_country** GeoId : Factor w/ 249 levels "AD","AE","AF",..: 1 2 3 4 5 6 7 8 9 10 ...
$ continent: Factor w/ 7 levels "--","AF","AN",..: 5 4 4 NA NA 5 4 NA 2 4 .
i want to bring them together by one extra column in Covid_19 continent. So when GeoId= AF (Afghanistan) in the new column i see in continent-column: AS (Asia)
How should i do this?

Best regards,
Rob

Hi, Rob

Hi, and welcome!

Please see the FAQ: What's a reproducible example (`reprex`) and how do I do one? Using a reprex, complete with representative data will attract quicker and more answers. The code here isn't enough to work the problem easily, so I'll just be able to point you the help(join) page in the dplyr package and show a toy example derived from the built-in mtcars dataset.

suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(tibble)) 
rownames_to_column(mtcars,var = "key") -> toy
toy %>% select(key,mpg,cyl,hp,drat,wt) -> toy1
toy %>% select(key,qsec,vs,am,gear) -> toy2
inner_join(toy1,toy2,by="key") -> toys
toys
#>                    key  mpg cyl  hp drat    wt  qsec vs am gear
#> 1            Mazda RX4 21.0   6 110 3.90 2.620 16.46  0  1    4
#> 2        Mazda RX4 Wag 21.0   6 110 3.90 2.875 17.02  0  1    4
#> 3           Datsun 710 22.8   4  93 3.85 2.320 18.61  1  1    4
#> 4       Hornet 4 Drive 21.4   6 110 3.08 3.215 19.44  1  0    3
#> 5    Hornet Sportabout 18.7   8 175 3.15 3.440 17.02  0  0    3
#> 6              Valiant 18.1   6 105 2.76 3.460 20.22  1  0    3
#> 7           Duster 360 14.3   8 245 3.21 3.570 15.84  0  0    3
#> 8            Merc 240D 24.4   4  62 3.69 3.190 20.00  1  0    4
#> 9             Merc 230 22.8   4  95 3.92 3.150 22.90  1  0    4
#> 10            Merc 280 19.2   6 123 3.92 3.440 18.30  1  0    4
#> 11           Merc 280C 17.8   6 123 3.92 3.440 18.90  1  0    4
#> 12          Merc 450SE 16.4   8 180 3.07 4.070 17.40  0  0    3
#> 13          Merc 450SL 17.3   8 180 3.07 3.730 17.60  0  0    3
#> 14         Merc 450SLC 15.2   8 180 3.07 3.780 18.00  0  0    3
#> 15  Cadillac Fleetwood 10.4   8 205 2.93 5.250 17.98  0  0    3
#> 16 Lincoln Continental 10.4   8 215 3.00 5.424 17.82  0  0    3
#> 17   Chrysler Imperial 14.7   8 230 3.23 5.345 17.42  0  0    3
#> 18            Fiat 128 32.4   4  66 4.08 2.200 19.47  1  1    4
#> 19         Honda Civic 30.4   4  52 4.93 1.615 18.52  1  1    4
#> 20      Toyota Corolla 33.9   4  65 4.22 1.835 19.90  1  1    4
#> 21       Toyota Corona 21.5   4  97 3.70 2.465 20.01  1  0    3
#> 22    Dodge Challenger 15.5   8 150 2.76 3.520 16.87  0  0    3
#> 23         AMC Javelin 15.2   8 150 3.15 3.435 17.30  0  0    3
#> 24          Camaro Z28 13.3   8 245 3.73 3.840 15.41  0  0    3
#> 25    Pontiac Firebird 19.2   8 175 3.08 3.845 17.05  0  0    3
#> 26           Fiat X1-9 27.3   4  66 4.08 1.935 18.90  1  1    4
#> 27       Porsche 914-2 26.0   4  91 4.43 2.140 16.70  0  1    5
#> 28        Lotus Europa 30.4   4 113 3.77 1.513 16.90  1  1    5
#> 29      Ford Pantera L 15.8   8 264 4.22 3.170 14.50  0  1    5
#> 30        Ferrari Dino 19.7   6 175 3.62 2.770 15.50  0  1    5
#> 31       Maserati Bora 15.0   8 335 3.54 3.570 14.60  0  1    5
#> 32          Volvo 142E 21.4   4 109 4.11 2.780 18.60  1  1    4

Created on 2020-04-08 by the reprex package (v0.3.0)

Thx, it was very helpfull. It took me a litlle time to implement in my case.
1 i had to rename a columnname (Countries and territories). this gave an error cause of the "and"
so my final code is now:

suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(tibble)) 
finalmatrix <- workingset
rownames_to_column(finalmatrix,var = "key") -> finalmatrix1
rownames_to_column(cont_country,var = "key") -> continents
finalmatrix1 %>% select(key,dateRep,cases,deaths,countryfull,geoId) -> finalmatrix1
continents %>% select(key,GeoId,continent) -> contcountry
inner_join(finalmatrix1,contcountry,by="key") -> finalend

thx for the tip of reprex, is also very helpfull
Kind regards and merry Eastern,
Rob

Hi, Rob

On of the things I do to make analysis easier is to save the original header names with

colnames(my_data) -> original_header

then

colnames(my_data) <- c("fi","fi',"fo","fum" ... )

and back again before displaying or saving

technocrat,

thx for your tips
this how a forum should work, learn and support from one another

Rob

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.