将条目添加到 R 中的多维列表
Add entries to multidimensional list in R
我有一个列表,它由多个列表组成。如何将 "newentry1"
和 "newentry2"
条目添加到所有这些多个条目中?所以,对于所有 my_list$Point
我应该有 my_list$Point$myentry1
和 my_list$Point$myentry2
my_list <- list(Point = list(position = "1", Production = "98.40",
imbalance_Price.category = "A06"), Point = list(position = "2",
Production = "0.00", imbalance_Price.category = "A06"),
Point = list(position = "3", Production = "0.00",
imbalance_Price.category = "A06"), Point = list(position = "4",
Production = "0.00", imbalance_Price.category = "A06"),
Point = list(position = "5", Production = "0.00",
imbalance_Price.category = "A06"), Point = list(position = "6",
Production = "188.70", imbalance_Price.category = "A06"))
最终结果应该是:
my_list <- list(Point = list(position = "1", Production = "98.40",
imbalance_Price.category = "A06",myentry1 = "A", myentry2 = "B"), Point = list(position = "2",
Production = "0.00", imbalance_Price.category = "A06",myentry1 = "A", myentry2 = "B"),
Point = list(position = "3", Production = "0.00",
imbalance_Price.category = "A06",myentry1 = "A", myentry2 = "B"), Point = list(position = "4",
Production = "0.00", imbalance_Price.category = "A06",myentry1 = "A", myentry2 = "B"),
Point = list(position = "5", Production = "0.00",
imbalance_Price.category = "A06",myentry1 = "A", myentry2 = "B"), Point = list(position = "6",
Production = "188.70", imbalance_Price.category = "A06",myentry1 = "A", myentry2 = "B"))
lapply(my_list, function(Point) {
Point$myentry1 <- "A"
Point$myentry2 <- "B"
Point
})
另一种可能的解决方案,基于purrr::map
:
library(purrr)
map(my_list, ~ append(.x, list(myentry1 = "A", myentry2 = "B")))
#> $Point
#> $Point$position
#> [1] "1"
#>
#> $Point$Production
#> [1] "98.40"
#>
#> $Point$imbalance_Price.category
#> [1] "A06"
#>
#> $Point$myentry1
#> [1] "A"
#>
#> $Point$myentry2
#> [1] "B"
#>
#>
#> $Point
#> $Point$position
#> [1] "2"
#>
#> $Point$Production
#> [1] "0.00"
#>
#> $Point$imbalance_Price.category
#> [1] "A06"
#>
#> $Point$myentry1
#> [1] "A"
#>
#> $Point$myentry2
#> [1] "B"
#>
#>
#> $Point
#> $Point$position
#> [1] "3"
#>
#> $Point$Production
#> [1] "0.00"
#>
#> $Point$imbalance_Price.category
#> [1] "A06"
#>
#> $Point$myentry1
#> [1] "A"
#>
#> $Point$myentry2
#> [1] "B"
#>
#>
#> $Point
#> $Point$position
#> [1] "4"
#>
#> $Point$Production
#> [1] "0.00"
#>
#> $Point$imbalance_Price.category
#> [1] "A06"
#>
#> $Point$myentry1
#> [1] "A"
#>
#> $Point$myentry2
#> [1] "B"
#>
#>
#> $Point
#> $Point$position
#> [1] "5"
#>
#> $Point$Production
#> [1] "0.00"
#>
#> $Point$imbalance_Price.category
#> [1] "A06"
#>
#> $Point$myentry1
#> [1] "A"
#>
#> $Point$myentry2
#> [1] "B"
#>
#>
#> $Point
#> $Point$position
#> [1] "6"
#>
#> $Point$Production
#> [1] "188.70"
#>
#> $Point$imbalance_Price.category
#> [1] "A06"
#>
#> $Point$myentry1
#> [1] "A"
#>
#> $Point$myentry2
#> [1] "B"
我有一个列表,它由多个列表组成。如何将 "newentry1"
和 "newentry2"
条目添加到所有这些多个条目中?所以,对于所有 my_list$Point
我应该有 my_list$Point$myentry1
和 my_list$Point$myentry2
my_list <- list(Point = list(position = "1", Production = "98.40",
imbalance_Price.category = "A06"), Point = list(position = "2",
Production = "0.00", imbalance_Price.category = "A06"),
Point = list(position = "3", Production = "0.00",
imbalance_Price.category = "A06"), Point = list(position = "4",
Production = "0.00", imbalance_Price.category = "A06"),
Point = list(position = "5", Production = "0.00",
imbalance_Price.category = "A06"), Point = list(position = "6",
Production = "188.70", imbalance_Price.category = "A06"))
最终结果应该是:
my_list <- list(Point = list(position = "1", Production = "98.40",
imbalance_Price.category = "A06",myentry1 = "A", myentry2 = "B"), Point = list(position = "2",
Production = "0.00", imbalance_Price.category = "A06",myentry1 = "A", myentry2 = "B"),
Point = list(position = "3", Production = "0.00",
imbalance_Price.category = "A06",myentry1 = "A", myentry2 = "B"), Point = list(position = "4",
Production = "0.00", imbalance_Price.category = "A06",myentry1 = "A", myentry2 = "B"),
Point = list(position = "5", Production = "0.00",
imbalance_Price.category = "A06",myentry1 = "A", myentry2 = "B"), Point = list(position = "6",
Production = "188.70", imbalance_Price.category = "A06",myentry1 = "A", myentry2 = "B"))
lapply(my_list, function(Point) {
Point$myentry1 <- "A"
Point$myentry2 <- "B"
Point
})
另一种可能的解决方案,基于purrr::map
:
library(purrr)
map(my_list, ~ append(.x, list(myentry1 = "A", myentry2 = "B")))
#> $Point
#> $Point$position
#> [1] "1"
#>
#> $Point$Production
#> [1] "98.40"
#>
#> $Point$imbalance_Price.category
#> [1] "A06"
#>
#> $Point$myentry1
#> [1] "A"
#>
#> $Point$myentry2
#> [1] "B"
#>
#>
#> $Point
#> $Point$position
#> [1] "2"
#>
#> $Point$Production
#> [1] "0.00"
#>
#> $Point$imbalance_Price.category
#> [1] "A06"
#>
#> $Point$myentry1
#> [1] "A"
#>
#> $Point$myentry2
#> [1] "B"
#>
#>
#> $Point
#> $Point$position
#> [1] "3"
#>
#> $Point$Production
#> [1] "0.00"
#>
#> $Point$imbalance_Price.category
#> [1] "A06"
#>
#> $Point$myentry1
#> [1] "A"
#>
#> $Point$myentry2
#> [1] "B"
#>
#>
#> $Point
#> $Point$position
#> [1] "4"
#>
#> $Point$Production
#> [1] "0.00"
#>
#> $Point$imbalance_Price.category
#> [1] "A06"
#>
#> $Point$myentry1
#> [1] "A"
#>
#> $Point$myentry2
#> [1] "B"
#>
#>
#> $Point
#> $Point$position
#> [1] "5"
#>
#> $Point$Production
#> [1] "0.00"
#>
#> $Point$imbalance_Price.category
#> [1] "A06"
#>
#> $Point$myentry1
#> [1] "A"
#>
#> $Point$myentry2
#> [1] "B"
#>
#>
#> $Point
#> $Point$position
#> [1] "6"
#>
#> $Point$Production
#> [1] "188.70"
#>
#> $Point$imbalance_Price.category
#> [1] "A06"
#>
#> $Point$myentry1
#> [1] "A"
#>
#> $Point$myentry2
#> [1] "B"