1

Is it possible to rename a list of lists elements?

For example:

fast <- subset.data.frame(mtcars, mtcars$hp > 250)
medium <- subset.data.frame(mtcars, mtcars$hp >200 & mtcars$hp < 300)
slow <- subset.data.frame(mtcars, mtcars$hp < 80)
mycars <- list(fast, medium, slow)

Gives a List of 3

mycars

[[1]]
                mpg cyl disp  hp drat   wt qsec vs am gear carb
Ford Pantera L 15.8   8  351 264 4.22 3.17 14.5  0  1    5    4
Maserati Bora  15.0   8  301 335 3.54 3.57 14.6  0  1    5    8

[[2]]
                     mpg cyl disp  hp drat    wt  qsec vs am gear carb
Duster 360          14.3   8  360 245 3.21 3.570 15.84  0  0    3    4
Cadillac Fleetwood  10.4   8  472 205 2.93 5.250 17.98  0  0    3    4
Lincoln Continental 10.4   8  460 215 3.00 5.424 17.82  0  0    3    4
Chrysler Imperial   14.7   8  440 230 3.23 5.345 17.42  0  0    3    4
Camaro Z28          13.3   8  350 245 3.73 3.840 15.41  0  0    3    4
Ford Pantera L      15.8   8  351 264 4.22 3.170 14.50  0  1    5    4

[[3]]
                mpg cyl  disp hp drat    wt  qsec vs am gear carb
Merc 240D      24.4   4 146.7 62 3.69 3.190 20.00  1  0    4    2
Fiat 128       32.4   4  78.7 66 4.08 2.200 19.47  1  1    4    1
Honda Civic    30.4   4  75.7 52 4.93 1.615 18.52  1  1    4    2
Toyota Corolla 33.9   4  71.1 65 4.22 1.835 19.90  1  1    4    1
Fiat X1-9      27.3   4  79.0 66 4.08 1.935 18.90  1  1    4    1

But I would like to rename the index [[1]] [[2]] [[3]] as [[fast]] [[medium]] [[slow]], OR preferable keep the index and also add the individual names of the list as an additional descriptor. Something like: [[1]]"Fast"

Scrolling through stackoverflow, similiar questions suggest renaming with names, but names(mycars) returns NULL.

names(mycars[[1]]) 
[1] "mpg"  "cyl"  "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear" "carb"

But I'm not interested in changing the column names.

This works:

 mycars <- list(fast = fast, medium = medium, slow = slow)

But, my actual dataset is more complicated, and I'm hoping to be able to rename the elements after creating the list.

moxed
  • 343
  • 1
  • 6
  • 16
  • When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions – MrFlick Feb 21 '18 at 21:14

2 Answers2

5
mycars <- setNames(mycars,c("fast","medium","slow"))

you can still use indexes :

mycars[[1]]
mycars[["fast"]]

names(mycars[[1]]) returns the column names of mycars[[1]].

One thing that might be counterintuitive is that a list in R has an optional names attribute of the same length as the number of columns, but the column items don't have their name attached to them (there's no singular name attribute).

By the time you call mycars[[1]] you lost the names. If you call mycars[1] however you get a list of one element only, and this list can have a names attribute of length one.

moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
  • Thanks for explaining! Very counter intuitive!. Indeed calling names(mycars[]) gives a list of all named ("fast", "medium", "slow") items. – moxed Feb 22 '18 at 21:23
2

You can add the names using the function names, for example:

names(data)[1:2] <- c("january", "february") 
Bruno Vilela
  • 103
  • 8