Home > Back-end >  Add entries to multidimensional list in R
Add entries to multidimensional list in R

Time:06-03

I have a list, which composed of multiple list. How can I add "newentry1" and "newentry2" entries to all of those multiple entries? So, for all my_list$Point I should have my_list$Point$myentry1 and 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"))

The final outcome should be:

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

CodePudding user response:

lapply(my_list, function(Point) {
  Point$myentry1 <- "A"
  Point$myentry2 <- "B"
  Point
})

CodePudding user response:

Another possible solution, based on 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"
  • Related