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.