【问题标题】:How to Use 'lapply' on Sublists (r)如何在子列表上使用“lapply”(r)
【发布时间】:2019-04-19 18:28:57
【问题描述】:

我正在尝试在子列表列表中使用 lapply。然而,我所能做的就是用应该进入它们的内容覆盖子列表,而不是写入子列表。

为了让我的问题清晰到单调乏味的程度,这与拥有一个“商店列表”相同,其中包含“DIY 商店”和“园艺商店”。试图将“Hammer and Nails”写入 DIY 子列表,将“Seeds”写入 Gardening 子列表,但意识到您实际上将所有物品都写入“Shopping List”,从而破坏了您的子列表。

我想有一种简单的方法可以告诉 R“哇,只需递归地查看我的第一层子列表”。希望这是一个简单的修复,并且更广泛的上下文不会过于有帮助,我的 lapply 部分以及我认为应该进行更改的地方就在下面。

# Create a list of Types and the matrices
StatMatrices <- lapply(Types, function(q) {
    # Select Versus List so that for example, a HESH list only contains HESHVersusHEAT<
    # HESHVerusHESH, HESHVersusAPDS and HESHVersusAPDR, and not HEATVersus.. and so on...
    WhichVersus <- grep(paste0("(^", q, ")"), VersusList, value = T, perl = T)
    EmptySublist <- (setNames(vector("list", length(Types)), WhichVersus))
  })
names(StatMatrices) <- Types

现在我尝试了一些方法,例如将 lapply 加倍,但我什至不确定我是否正确地这样做,更不用说它是否是正确的解决方案。

# Create a list of Types and the matrices
StatMatrices <- lapply(Types,
                       lapply, function(q) {
    # Select Versus List so that for example, a HESH list only contains HESHVersusHEAT<
    # HESHVerusHESH, HESHVersusAPDS and HESHVersusAPDR, and not HEATVersus.. and so on...
    WhichVersus <- grep(paste0("(^", q, ")"), VersusList, value = T, perl = T)
    EmptySublist <- (setNames(vector("list", length(Types)), WhichVersus))
  })
names(StatMatrices) <- Types

我的完整代码如下所示。我试图评论它,但基本上我有 4 个“类型”,我正在创建一个名为 StatMatrices 的列表。这将包含更多列表,下一层将包含名称,例如VelStatMatrices,根据后续信息命名,将在其中存储。下一组列表将是所谓的类型(即HEATHESHAPDSAPCR)。这些将包含比较矩阵,尽管现在只是占位符名称。因此,在HESH 内部,您将有HESHVersusHEAT HESHVerusHESHHESHVersusAPDSHESHVersusAPDR 等待填充矩阵。

最终的列表路径如下所示:

StatMatrices[["VelStatMatrices"]][[HESH]][[HESHVerusHEAT]]

还有一个这样的

StatMatrices[["PenStatMatrices"]][["APDS"]][["APDSVerusHESH"]]

这是我目前所拥有的:

#Mun Types
Types = c("HEAT", "HESH", "APDS", "APCR")

#Create empty vector for Versus name strings
VersusList <- c()

#Create Versus names e.g. HEATVerusAPDS and APCRVersusHESH etc
for (q in 1:length(Types)) {
  for (j in 1:length(Types)) {

    # VersusList(i) <- paste0(Types[q], "Versus", Types[j])
    VersusList <- c(VersusList, paste0(Types[q], "Versus", Types[j]))
  }
}

#Create List of lists of stat matrices, each to be filled with matrices,  
StatMatrices <- list("PenStatMatrices", "DmgStatMatrices", "VelStatMatrices")

# Create a list of Types and the matrices
StatMatrices <- lapply(Types, function(q) {
    # Select Versus List so that for example, a HESH list only contains HESHVersusHEAT<
    # HESHVerusHESH, HESHVersusAPDS and HESHVersusAPDR, and not HEATVersus.. and so on...
    WhichVersus <- grep(paste0("(^", q, ")"), VersusList, value = T, perl = T)
    EmptySublist <- (setNames(vector("list", length(Types)), WhichVersus))
  })
names(StatMatrices) <- Types

我才刚刚开始使用 lapply,几乎不知道我在用 R 做什么,所以任何帮助都将不胜感激。

【问题讨论】:

  • VersusList在哪里(稍后显示,但代码太多很难找到)?!你能让你的帖子更简洁吗?

标签: r list loops lapply sublist


【解决方案1】:

很抱歉此时没有提供更具体的答案(也许更简洁的问题会有所帮助),但我建议您查看utils::modifyList()。似乎这会有所帮助。

另外,看看你的用例,你可能会为自己构建一些不同的对象而省去一些痛苦,一个例子是矩阵矩阵:

制作一个由 4x4 矩阵组成的 4x4 矩阵(用于展示的随机数):

mtr <- matrix(
  rep(list(matrix(runif(16L), nrow = 4L)), 16L),
  nrow = 4,
  ncol = 4
)

设置行和列名以提高可读性

rownames(mtr) <- colnames(mtr) <-  c("HEAT", "HESH", "APDS", "APCR")

然后像这样访问数据:

mtr["HEAT", "HEAT"]

给了

[[1]]
            [,1]      [,2]      [,3]      [,4]
[1,] 0.413062588 0.2497092 0.6465910 0.9492033
[2,] 0.007168949 0.9049881 0.1491240 0.1494439
[3,] 0.540454187 0.4837212 0.2674728 0.6261278
[4,] 0.327341522 0.9747880 0.7509647 0.5489474

你有一个矩阵列表,所以你可以很容易地应用函数:

lapply(mtr, summary)

返回(截断):

[[1]]
       V1                 V2               V3               V4        
 Min.   :0.007169   Min.   :0.2497   Min.   :0.1491   Min.   :0.1494  
 1st Qu.:0.247298   1st Qu.:0.4252   1st Qu.:0.2379   1st Qu.:0.4491  
 Median :0.370202   Median :0.6944   Median :0.4570   Median :0.5875  
 Mean   :0.322007   Mean   :0.6533   Mean   :0.4535   Mean   :0.5684  
 3rd Qu.:0.444910   3rd Qu.:0.9224   3rd Qu.:0.6727   3rd Qu.:0.7069  
 Max.   :0.540454   Max.   :0.9748   Max.   :0.7510   Max.   :0.9492  

[[2]]
       V1                 V2               V3               V4        
 Min.   :0.007169   Min.   :0.2497   Min.   :0.1491   Min.   :0.1494  
 1st Qu.:0.247298   1st Qu.:0.4252   1st Qu.:0.2379   1st Qu.:0.4491  

...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-30
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 2013-02-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多