Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to name Dataframes inside a List

Tags:

list

dataframe

r

I have created a basic list, and inside this list called lista (not big fantasy I know) there are 10 small dataframes. Each one of this dataframes is called "numberone","numbertwo",...,"numberten".

When I accede this list I can't see their names. but the output I can see in the workspace (Rstudio) is this

enter image description here

This below is the code and my tries:

#creating multiple dataframes and a list and then give a title to this dataframes inside the list.

lista = list()
names = c("numberone","numbertwo","numberthree","numberfour","numberfive","numbersix","numberseven","numbereight","numbernine","numberten")

for (i in 1:10) {
  x = rnorm(10)
  df = data.frame(x)
  assign(names[i],df)
  lista[[i]] = df
}


#trying to change manually the names of the dataframes inside the "lista"  list

names(lista[1]) = "number one"
print(names(lista[1]))  #this gives no results

#trying using dput
output = dput(lista[1])

##trying put manually the name in front of the dput output to rename the first dataframe inside lista..
list('numberone'= structure(list(x = c(0.750704535096297, 1.16925878942967, 
                          0.806475114411396, 1.00973486249489, -0.301553383694518, 0.546485320708262, 
                          1.03645444095639, 0.247820396853631, -1.64294545886444, -0.216784798035195
)), class = "data.frame", row.names = c(NA, -10L)))

#this seems to have renamed the first dataframe but, it's not working anyway
lista$numberone

print(names(lista[1])) #still no results

I've tried almost everything I could, but I can't give this single dataframes their names inside the list.

How can i name these dataframes? Thank You

like image 513
Carlo Avatar asked Jul 18 '26 10:07

Carlo


1 Answers

Try to do names(list)

Here an example using empty lists

list_test = vector("list",4)
names(list_test) = c("A","B","C","D")
list_test

$A
NULL

$B
NULL

$C
NULL

$D
NULL

With your example, I did:

names(lista) <- names

and I get:

names(lista)
[1] "numberone"   "numbertwo"   "numberthree" "numberfour"  "numberfive"  "numbersix"   "numberseven"
[8] "numbereight" "numbernine"  "numberten"   
like image 156
dc37 Avatar answered Jul 21 '26 02:07

dc37



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!