emperorpointertine123
emperorpointertine123

Reputation: 35

How to merge two columns with the same names from two different data frames and compare and print the ones that are similar

I currently have this code:

install.packages(c("httr", "jsonlite", "tidyverse"))
library(httr)
library(jsonlite)
library(tidyverse)

res1<-GET("https://rss.applemarketingtools.com/api/v2/us/music/most-played/100/songs.json")
res1
rawToChar(res1$content)

data1 = fromJSON(rawToChar(res1$content))

us100<-data1$feed$results

res2 <- GET("https://rss.applemarketingtools.com/api/v2/gb/music/most-played/100/songs.json")

data2<-fromJSON(rawToChar(res2$content))
uk100<-data2$feed$results

I want to compare the two data frames and make a new one printing the results of the artist names and name of the songs that both data frames have in common, how do I do this?

Upvotes: 0

Views: 69

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 174506

I think you're just looking for an inner_join

us100 %>% inner_join(uk100, by = "id") %>% as_tibble()
#> # A tibble: 16 x 21
#>    artistName.x         id    name.x releaseDate.x kind.x artistId.x artistUrl.x
#>    <chr>                <chr> <chr>  <chr>         <chr>  <chr>      <chr>      
#>  1 Jack Harlow          1618~ First~ 2022-04-08    songs  1047679432 https://mu~
#>  2 Harry Styles         1615~ As It~ 2022-03-31    songs  471260289  https://mu~
#>  3 Lil Baby             1618~ In A ~ 2022-04-08    songs  1276656483 https://mu~
#>  4 Lauren Spencer-Smith 1618~ Flowe~ 2022-04-14    songs  1462708784 https://mu~
#>  5 Glass Animals        1508~ Heat ~ 2020-06-29    songs  528928008  https://mu~
#>  6 Carolina Gaitán - ~ 1594~ We Do~ 2021-11-19    songs  1227636438 https://mu~
#>  7 The Kid LAROI & Jus~ 1574~ STAY   2021-07-09    songs  1435848034 https://mu~
#>  8 Frank Ocean          1440~ Lost   2012-07-10    songs  442122051  https://mu~
#>  9 Elton John & Dua Li~ 1578~ Cold ~ 2021-08-13    songs  54657      https://mu~
#> 10 Tate McRae           1606~ she's~ 2022-02-04    songs  1446365464 https://mu~
#> 11 Adele                1590~ Easy ~ 2021-10-14    songs  262836961  https://mu~
#> 12 Lil Tjay             1613~ In My~ 2022-04-01    songs  1436446949 https://mu~
#> 13 Lizzo                1619~ About~ 2022-04-14    songs  472949623  https://mu~
#> 14 Tiësto & Ava Max    1590~ The M~ 2021-11-04    songs  4091218    https://mu~
#> 15 Ed Sheeran           1581~ Shive~ 2021-09-09    songs  183313439  https://mu~
#> 16 The Weeknd           1488~ Blind~ 2019-11-29    songs  479756766  https://mu~
#> # ... with 14 more variables: contentAdvisoryRating.x <chr>,
#> #   artworkUrl100.x <chr>, genres.x <list>, url.x <chr>, artistName.y <chr>,
#> #   name.y <chr>, releaseDate.y <chr>, kind.y <chr>, artistId.y <chr>,
#> #   artistUrl.y <chr>, contentAdvisoryRating.y <chr>, artworkUrl100.y <chr>,
#> #   genres.y <list>, url.y <chr>
``z

Upvotes: 1

Related Questions