Mutating a column of lists to a data frame

How would I go about mutating a new variable called tracks to a data frame, where each row's value for tracks is the following list?

list(track_title = character(0), lyric = character(0), line = integer(0))

Here's an example:

artists <- c("Spiritualized", "Ween", "Death Grips")
albums <- c("Pure Phase", "Quebec", "The Money Store")
mydata <- data_frame(artists, albums)

mydata

# A tibble: 3 x 2
  artists       albums         
  <chr>         <chr>          
1 Spiritualized Pure Phase     
2 Ween          Quebec         
3 Death Grips   The Money Store

I guess what I'm trying to do is end up with this:

# A tibble: 3 x 3
  artists       albums         tracks
  <chr>         <chr>          <list>
1 Spiritualized Pure Phase     list(track_title = character(0), lyric = character(0), line = integer(0))
2 Ween          Quebec         list(track_title = character(0), lyric = character(0), line = integer(0))
3 Death Grips   The Money St~  list(track_title = character(0), lyric = character(0), line = integer(0))

I know this is a weird way to go about things, but I kinda need my data formatted this way. Normal mutate(mydata, tracks = list(...)) doesn't work here, so I think I need some map-related solution.

Any ideas?

2 Likes

Do you mean like this?

suppressPackageStartupMessages(library(tidyverse))

artists <- c("Spiritualized", "Ween", "Death Grips")
albums <- c("Pure Phase", "Quebec", "The Money Store")
mydata <- data_frame(artists, albums)

foo <- list(track_title = character(0), lyric = character(0), line = integer(0))


mydata2<- mydata %>%
  mutate(track = list(foo))

mydata2
#> # A tibble: 3 x 3
#>   artists       albums          track     
#>   <chr>         <chr>           <list>    
#> 1 Spiritualized Pure Phase      <list [3]>
#> 2 Ween          Quebec          <list [3]>
#> 3 Death Grips   The Money Store <list [3]>

Created on 2018-05-11 by the reprex package (v0.2.0).

2 Likes

whelp -- that's embarrassing. Thanks, Mara!

(I realized I am not answering the question and am editing and will repost).

Edit: actually my thoughts were totally off and mara, not only answered exactly your question, but she also gave the simplest answer :slight_smile:

Note: you should mark her answer as the solution to your question by ticking the little square :slight_smile:

Re-edit: ah! you beat me to it. Thank you!!

I'm curious, why is this? No judgements; we've all had to make H.R.-Geiger-ish "sculptures" of data for what came next.

yeah I feel that.

right now it's because i'm dealing with lyric data from the geniusR package, and lyrics are outputted as a list of lists. And right now I'm joining a data frame of instrumental songs (so no lyrics), and, for consistency's sake, I wanted to add this specific list of lists to those songs.

And also I'm in a pretty huge rush to finish this (due tomorrow!), so I'm just going with what I've got