【问题标题】:Extract element from nested list based on column name根据列名从嵌套列表中提取元素
【发布时间】:2020-03-17 21:42:32
【问题描述】:

我试图根据列表中列名的值从嵌套数字矩阵中提取单个元素。这是数据的示例。我正在考虑使用正则表达式将权重列表的列与 ets、arima 和 theta 的预测集合列相匹配?

当前

ets         arima        theta            list_of_weights
forecast.   forecast    forecast    (ets_weight:0.5, arima_weight:0.2,theta_weight:0.3)
forecast    forecast    forecast    (ets_weight:0.4, arima_weight:0.1,theta_weight:0.5)

理想

model_type  model_fcst  weights
ets         forecast    0.5
ets         forecast    0.4
arima       forecast    0.2
arima       forecast    0.1
theta       forecast    0.3
theta       forecast    0.5

【问题讨论】:

  • 您有list_of_weights 作为字符列还是list 列?可以使用dput 提供数据吗?

标签: r dataframe statistics


【解决方案1】:

我们。可以转换。在unnest'list_of_weights' 之后使用pivot_longer 进行长格式,然后使用parse_number 从'list_of_weights' 列中获取权重

library(dplyr)
library(tidyr)
library(stringr)
df1 %>%        
    unnest(c(list_of_weights)) %>%
    pivot_longer(cols = ets:theta, names_to = 'model_type', 
                   values_to = 'model_fcst') %>%
    filter(str_detect(list_of_weights, model_type)) %>% 
    mutate(weights = readr::parse_number(list_of_weights)) %>% 
    select(-list_of_weights)
# A tibble: 6 x 3
#  model_type model_fcst weights
#  <chr>      <chr>        <dbl>
#1 ets        forecast       0.5
#2 arima      forecast       0.2
#3 theta      forecast       0.3
#4 ets        forecast       0.4
#5 arima      forecast       0.1
#6 theta      forecast       0.5

数据

df1 <- structure(list(ets = c("forecast", "forecast"), arima = c("forecast", 
"forecast"), theta = c("forecast", "forecast"), list_of_weights = list(
    c("ets_weight:0.5", "arima_weight:0.2", "theta_weight:0.3"
    ), c("ets_weight:0.4", "arima_weight:0.1", "theta_weight:0.5"
    ))), row.names = c(NA, -2L), class = "data.frame")

【讨论】:

    猜你喜欢
    • 2016-07-02
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2015-10-15
    • 1970-01-01
    • 1970-01-01
    • 2022-11-12
    • 2017-01-18
    相关资源
    最近更新 更多