如何从嵌套或列表列中提取或取消嵌套未命名的值?

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

下面的 df 有一个列表列 (CVcCVq),它只有一个数值。我想将这个单一值提取到另一列或将此列表列转换为数字列。

我过去曾使用下面的代码提取列表的元素(例如,通过使用 .x$value),但我不知道如何引用 (CVcCVq):

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

这是我正在使用的数据框示例:

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"))

您可以在此处使用unlist()unnest() 或使用子集[[ 提取。 我建议您在这里坚持使用 unnest()。如果您有多个嵌套列表列,它会更加一致和清晰,并且更易于使用。

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

您也可以使用自己代码的变体:

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