Search code examples
rlisttidyversetidyrpurrr

How to extract or unnest an unnamed value from a nested or list column?


The following df has a list column (CVcCVq) that has a single numeric value. I would like to extract this single value into another column or convert this list column into a numeric column.

I have used the below code to extract elements of a list in the past (e.g., by using .x$value), but I cannot figure out how to reference the unnamed(?) value in (CVcCVq):

df <-
   df %>% 
   mutate(CVcCVq = map_dbl(CVcCVq, ~ .x$value))

Here is a sample of the dataframe I'm working with:

df <- 
   structure(list(site = c("Hungerford", "Hungerford", "Hungerford", 
"Hungerford", "Hungerford", "Hungerford"), date = structure(c(16244, 
16244, 16244, 16244, 16245, 16245), class = "Date"), q = c(0.13302763934, 
0.13302763934, 0.13302763934, 0.13302763934, 0.118154355, 0.118154355
), year = c(2014, 2014, 2014, 2014, 2014, 2014), var = c("DOC", 
"NO3", "SRP", "turb", "DOC", "NO3"), value = c(8.41162692329658, 
2.68458225207895, 0.0100915159605364, 8.0213, 8.23726061695833, 
2.49696316297646), CVcCVq = list(0.129399469450364, 0.504972938773432, 
    1.13463616961327, 0.602451097752468, 0.129399469450364, 0.504972938773432)), row.names = c(NA, 
-6L), groups = structure(list(site = c("Hungerford", "Hungerford", 
"Hungerford", "Hungerford"), year = c(2014, 2014, 2014, 2014), 
    var = c("DOC", "NO3", "SRP", "turb"), .rows = structure(list(
        c(1L, 5L), c(2L, 6L), 3L, 4L), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = c(NA, -4L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

Solution

  • You can use unlist(), unnest(), or extract with subsetting [[ here. I recommend you stick with unnest() here. It is more consistent and clear, and easier to use if you have multiple nested list columns.

    library(dplyr)
    
    df %>% mutate(CVcCVq = unlist(CVcCVq))
    
    #OR
    
    library(dplyr)
    library(tidyr)
    
    df %>% unnest(cols = CVcCVq)
    
    #OR
    
    df %>% mutate(CVcCVq = `[[`(CVcCVq, 1))
    
    #OR
    
    df %>% mutate(CVcCVq = CVcCVq[[1]])
    
    
    # A tibble: 6 x 7
    # Groups:   site, year, var [4]
      site       date           q  year var    value CVcCVq
      <chr>      <date>     <dbl> <dbl> <chr>  <dbl>  <dbl>
    1 Hungerford 2014-06-23 0.133  2014 DOC   8.41    0.129
    2 Hungerford 2014-06-23 0.133  2014 NO3   2.68    0.505
    3 Hungerford 2014-06-23 0.133  2014 SRP   0.0101  1.13 
    4 Hungerford 2014-06-23 0.133  2014 turb  8.02    0.602
    5 Hungerford 2014-06-24 0.118  2014 DOC   8.24    0.129
    6 Hungerford 2014-06-24 0.118  2014 NO3   2.50    0.505
    

    You can also use a variant of your own code:

    df%>%mutate(CVcCVq=map_dbl(CVcCVq, ~.x))