So I initialize a list, which I want to fill with dataframes:
listz <- vector("list",2)
I also want to keep the dataframes’ names around, so I assign them:
listzNames <- c("frame1","frame2")
names(listz) <- listzNames
The problem is, with every reassignment I make to the dataframes, the names go NULL:
listz <- list(data.frame("id" = 1:3, "hat" = 1:3),
data.frame("id" = 4:6, "hat" = 4:6))
> names(listz)
NULL
I know why this happens, but what would be a neat alternative to reassigning the names at every dataframe reassignment?
When you assign
You are replacing the object formerly defined as
listz, it is a new object, unrelated to any previous objects of that name.Therefore is no need to initialize the list in this case
you have (at least) four options for setting names of a list
option 1 –
setNamesoption 2 – name as you go
option 3 –
Hmiscandllistoption 4 – prexisting using setNames and get
option 5 initializing the list (I don’t like this)
I don’t like this option, it requires more typing and therefore more chance of errors!
Note about
lapplylapplywill preserve any names