我对列表相当陌生,以前只处理矩阵和数据帧.下面的MWE代码中的createBucket()函数读取dfList列表,并创建由dfList中的"df_one"和"df_Two"元素组成的数据帧的扩展列表.它正确地做到了这一点.然而,我在子列表数据帧名称方面遇到了麻烦.我目前只能通过索引号访问DataFrame,例如运行tMP[1]从列表中提取第一个DataFrame.如何将父/子 struct 添加到命名中,例如,如果我运行类似tmp$df_one(或tmp[["df_one"]的命令,我将获得所有三个df_one数据帧,而运行tmp$df_one$Boy将在"df_one"父数据帧下提供"Boy"子数据帧?

理想情况下,保留数字索引作为提取子列表数据帧的替代方法也很好.如果这是可能的.如果不可能,最重要的是能够按照第一段中描述的字符串名称访问数据帧.

我需要良好的命名/索引,因为这个示例所针对的较大代码最终是跨子列表数据帧的计算密集型代码.在执行计算时,一个子列表数据帧中的一列将需要与另一个子列表数据帧中的另一列(或列/行组合)进行通信.为了能够进行这些计算,我需要一个健壮的命名层次 struct .

代码:

dfVector <- function() {c("DF_One", "DF_Two")}
dfList <- list(DF_One = c("Boy", "Cat", "Dog"), DF_Two = c("Boy", "Rat", "Bat"))

createBucket <- function(nbr_rows) {
  series <- dfVector()
  series |>
    lapply(FUN = \(series_name) {
      lapply(dfList[[series_name]], \(element) {
        df <- as.data.frame(matrix(0, nbr_rows, 2))
        colnames(df) <- c('A', 'B')
        setNames(list(df), paste0(series_name, "_", element))
      })
    }) |>
    unlist(recursive = FALSE)
}

tmp <- createBucket(5)

推荐答案

你想要这样的东西(修改你的功能)吗?

createBucket <- function(nbr_rows) {
  series <- dfVector()
  setNames(
    lapply(series, \(series_name) {
      setNames(
        lapply(dfList[[series_name]], \(element) {
          df <- as.data.frame(matrix(0, nbr_rows, 2))
          colnames(df) <- c('A', 'B')
          df
        }),
        dfList[[series_name]]
      )
    }),
    series
  )
}

tmp <- createBucket(5)
tmp
$DF_One
$DF_One$Boy
  A B
1 0 0
2 0 0
3 0 0
4 0 0
5 0 0

$DF_One$Cat
  A B
1 0 0
2 0 0
3 0 0
4 0 0
5 0 0

$DF_One$Dog
  A B
1 0 0
2 0 0
3 0 0
4 0 0
5 0 0


$DF_Two
$DF_Two$Boy
  A B
1 0 0
2 0 0
3 0 0
4 0 0
5 0 0

$DF_Two$Rat
  A B
1 0 0
2 0 0
3 0 0
4 0 0
5 0 0

$DF_Two$Bat
  A B
1 0 0
2 0 0
3 0 0
4 0 0
5 0 0

EDIT:添加访问列表/子列表中的值的指南-

tmp$DF_One # renders all 3 sublists under $DF_One parent
tmp[1] # renders all 3 sublists under $DF_One including parent list name "$DF_One"
tmp[[1]] # renders all 3 sublists under $DF_One excluding parent list name "$DF_One"
tmp$DF_One[1] # renders first sublist dataframe under $DF_One parent including sublist name "Boy"
tmp$DF_One[[1]] # renders first sublist dataframe under $DF_One parent excluding sublist name "Boy"
tmp[[1]][1] # renders first sublist dataframe under $DF_One parent including sublist name "Boy"
tmp[[1]][[1]] # renders first sublist dataframe under $DF_One parent excluding sublist name "Boy"
tmp$DF_One$Cat$B[3] # access value in col B/row 3 of 2nd sublist dataframe "Cat" under parent "DF_One" 
tmp$DF_One$Cat$B <- 8 # change all values in col B of 2nd sublist dataframe "Cat" under parent "DF_One" to 8
tmp$DF_One$Cat$B[3] <- 8 # change value in col B/row 3 of 2nd sublist dataframe "Cat" under parent "DF_One" to 8

R相关问答推荐

为什么stat_bin在R中的ggplot中显示错误的数字?

如何将具有重复名称的收件箱合并到R中的另一列中,而结果不同?

R的GG平行坐标图中的排序变量

使用对管道内单元格的引用生成新变量

如何将在HW上运行的R中的消息(错误、警告等)作为批处理任务输出

MCMC和零事件二元逻辑回归

获取列中值更改的行号

在特定Quarto(reveal.js)幻灯片上隐藏徽标

根据文本字符串中的值粘贴新列

即使硬币没有被抛出,也要保持对其的跟踪

提取一个列表中单个列的重复观察结果R

`lazy_dt`不支持`dplyr/across`?

以相同的方式对每个表进行排序

R -在先前group_by级别汇总时获取最大大小子组的计数

基于数据集属性将科分配给物种

随机森林的带Shap值的蜂群图

R中Gamma回归模型均方误差的两种计算方法不一致

如何提取R中其他字符串和数字之间的字符串?

随机 Select 的非NA列的行均数

按两个因素将观测值分组后计算单独的百分比