2

After some manipulation, I have a list with the following elements:

$default
   a      b      c      d      e 
 1e-05  1e-02  1e-02  0e+00  0e+00 

$`1`
     a                                 d          e
   0.00001   35.62987 4252.45832    0.00000    0.00000 

$`2`
     a                                 d          e
   0.00001   35.62987 4252.45832    0.00000    0.00000 

The list can be reproduced with:

list1 <- list("default" = c("a" = 0.00001, "b" = 0.01, "c" = .01, "d"=0, "e"=0),
                            "1" = c("a" = 0.00001, 35.62987, 4252.45832, "d"=0, "e"=0),
              "2" = c("a" = 0.00001, 35.62987, 4252.45832, "d"=0, "e"=0))

I'd like to quickly rename the elements of "1" and "2" to have the same names as the elements of the "standard" element i.e. "a", "b", "c", "d", "e."

But when I use lapply to reassign names, the elements of the list are lost. When I do:

lapply(list1, function(x) names(x) <- c("a", "b", "c", "d", "e"))

I get:

$default
[1] "a"    "b"      "c"      "d"    "e"

$`1`
[1] ""a"    "b"      "c"      "d"    "e"

$`2`
[1] "a"    "b"      "c"      "d"    "e"

such that all the actual values are lost. Is there a better way to do renaming across list elements?

Gabe Solomon
  • 365
  • 3
  • 12
  • Hello and welcome! To make it easier for us, please provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Eric Jan 27 '21 at 20:43
  • Thanks! That makes total sense. I've edited my question to add a reproducible example. – Gabe Solomon Jan 27 '21 at 20:54
  • I have updated my answer to include your reproducible example. If it is correct, please consider marking it as the solution by clicking the checkmark. Thanks! – Eric Jan 27 '21 at 20:58

3 Answers3

1

We need to return the data. In the OP's code, it is returning the output of the last assignment

list1 <- lapply(list1, function(x) {
           names(x) <- c("a", "b", "c", "d", "e")
        x})
akrun
  • 874,273
  • 37
  • 540
  • 662
1
list1 <- list("default" = c("a" = 0.00001, "b" = 0.01, "c" = .01, "d"=0, "e"=0),
                            "1" = c("a" = 0.00001, 35.62987, 4252.45832, "d"=0, "e"=0),
              "2" = c("a" = 0.00001, 35.62987, 4252.45832, "d"=0, "e"=0))

Original list:

$default
    a     b     c     d     e 
1e-05 1e-02 1e-02 0e+00 0e+00 

$`1`
         a                                d          e 
   0.00001   35.62987 4252.45832    0.00000    0.00000 

$`2`
         a                                d          e 
   0.00001   35.62987 4252.45832    0.00000    0.00000 

Code:

lapply(list1, function(x) setNames(x, c("a", "b", "c", "d", "e")))

Updated list:

#> $default
#>     a     b     c     d     e 
#> 1e-05 1e-02 1e-02 0e+00 0e+00 
#> 
#> $`1`
#>          a          b          c          d          e 
#>    0.00001   35.62987 4252.45832    0.00000    0.00000 
#> 
#> $`2`
#>          a          b          c          d          e 
#>    0.00001   35.62987 4252.45832    0.00000    0.00000

Created on 2021-01-27 by the reprex package (v0.3.0)

Eric
  • 2,699
  • 5
  • 17
1

Perhaps you can try setNames

lapply(list1, setNames, c("a", "b", "c", "d", "e"))
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81