I have the dataframe below which in column genres
has nested dataframes with 3 columns. I wonder how can I find how many times the word "Pop"
is displayed in the column name
of all nested dataframes that exist in column genres
dat<-structure(list(name = c("Easy On Me", "All I Want For Christmas Is You",
"Overseas (feat. Central Cee)", "Last Christmas", "Shivers"),
releaseDate = c("2021-10-15", "1994-10-29", "2021-11-18",
"1984-01-01", "2021-09-09"), kind = c("songs", "songs", "songs",
"songs", "songs"), artistId = c("262836961", "91853", "1240341559",
"548421", "183313439"), artistUrl = c("https://music.apple.com/gb/artist/adele/262836961",
"https://music.apple.com/gb/artist/mariah-carey/91853", "https://music.apple.com/gb/artist/d-block-europe/1240341559",
"https://music.apple.com/gb/artist/wham/548421", "https://music.apple.com/gb/artist/ed-sheeran/183313439"
), artworkUrl100 = c("https://is3-ssl.mzstatic.com/image/thumb/Music115/v4/73/6d/7c/736d7cfb-c79d-c9a9-4170-5e71d008dea1/886449666430.jpg/100x100bb.jpg",
"https://is4-ssl.mzstatic.com/image/thumb/Music124/v4/c6/b7/27/c6b727f7-3a32-6b43-cee2-05bb71daf1cf/dj.itfmdeif.jpg/100x100bb.jpg",
"https://is1-ssl.mzstatic.com/image/thumb/Music126/v4/2e/63/01/2e6301ee-905d-5ae8-c989-eaf9d8e7e6ae/21UM1IM30658.rgb.jpg/100x100bb.jpg",
"https://is1-ssl.mzstatic.com/image/thumb/Music125/v4/47/55/0c/47550cd6-7ef5-bf86-c194-c7695d63c759/dj.xuditatj.jpg/100x100bb.jpg",
"https://is1-ssl.mzstatic.com/image/thumb/Music125/v4/c5/d8/c6/c5d8c675-63e3-6632-33db-2401eabe574d/190296491412.jpg/100x100bb.jpg"
), genres = list(structure(list(genreId = c("14", "34"),
name = c("Pop", "Music"), url = c("https://itunes.apple.com/gb/genre/id14",
"https://itunes.apple.com/gb/genre/id34")), class = "data.frame", row.names = 1:2),
structure(list(genreId = c("34", "21", "17", "15", "14"
), name = c("Music", "Rock", "Dance", "R&B/Soul", "Pop"
), url = c("https://itunes.apple.com/gb/genre/id34",
"https://itunes.apple.com/gb/genre/id21", "https://itunes.apple.com/gb/genre/id17",
"https://itunes.apple.com/gb/genre/id15", "https://itunes.apple.com/gb/genre/id14"
)), class = "data.frame", row.names = c(NA, 5L)), structure(list(
genreId = c("18", "34"), name = c("Hip-Hop/Rap",
"Music"), url = c("https://itunes.apple.com/gb/genre/id18",
"https://itunes.apple.com/gb/genre/id34")), class = "data.frame", row.names = 1:2),
structure(list(genreId = c("14", "34", "17"), name = c("Pop",
"Music", "Dance"), url = c("https://itunes.apple.com/gb/genre/id14",
"https://itunes.apple.com/gb/genre/id34", "https://itunes.apple.com/gb/genre/id17"
)), class = "data.frame", row.names = c(NA, 3L)), structure(list(
genreId = c("14", "34"), name = c("Pop", "Music"),
url = c("https://itunes.apple.com/gb/genre/id14",
"https://itunes.apple.com/gb/genre/id34")), class = "data.frame", row.names = 1:2))), row.names = c(NA,
5L), class = "data.frame")
CodePudding user response:
If you want the total number of times the word Pop
appears in the genres;
library(tidyverse)
sum(bind_rows(dat$genres)['name'] == 'Pop')
[1] 4
if you want the number of times in each nested dataframe:
map_dbl(dat$genres, ~sum(.x['name']=='Pop'))
[1] 1 1 0 1 1
CodePudding user response:
mapply(`%in%`, "Pop", lapply(dat$genres, `[[`, "name"))
# Pop <NA> <NA> <NA> <NA>
# TRUE TRUE FALSE TRUE TRUE