Home > Enterprise >  getting the names of data frames from list in R
getting the names of data frames from list in R

Time:12-28

I have a list which contains 36 data frames. I want to create a list containing all the names of those data frames :

   dput(myfiles[1:2])
list(structure(list(X.Treatment.1.Treatment.10.Treatment.2.Treatment.3.Treatment.4.Treatment.5.Treatment.6.Treatment.7.Treatment.8.Treatment.9 = c("Treatment.1,1,0.779269898976048,0.987582177817029,0.999865208543176,0.999637376053903,0.969316946773183,0.992798203986959,0.424960684181985,0.804869101320034,0.934784678841289", 
"Treatment.10,0.779269898976048,1,0.671138248567996,0.789454098761072,0.762111859396959,0.909408486972833,0.848734212632234,-0.236126723371631,0.255300504533133,0.505840502482398", 
"Treatment.2,0.987582177817029,0.671138248567996,1,0.984869671366683,0.991454531822078,0.918661911614817,0.961649044703906,0.561895346303209,0.888107698459535,0.978982111839266", 
"Treatment.3,0.999865208543176,0.789454098761072,0.984869671366683,1,0.99906051831384,0.973222174821046,0.994631289318653,0.410041249133801,0.795017057233326,0.9288266084351", 
"Treatment.4,0.999637376053903,0.762111859396959,0.991454531822078,0.99906051831384,1,0.962346166096083,0.989212254209048,0.449182113577399,0.820557713571369,0.944010924367408", 
"Treatment.5,0.969316946773183,0.909408486972833,0.918661911614817,0.973222174821046,0.962346166096083,1,0.991784351747349,0.189407610662142,0.634294194129571,0.81878574572229", 
"Treatment.6,0.992798203986959,0.848734212632234,0.961649044703906,0.994631289318653,0.989212254209048,0.991784351747349,1,0.31345701514879,0.72797778020465,0.885498274066011", 
"Treatment.7,0.424960684181985,-0.236126723371631,0.561895346303209,0.410041249133801,0.449182113577399,0.189407610662142,0.31345701514879,1,0.879237827530393,0.718791431723663", 
"Treatment.8,0.804869101320034,0.255300504533133,0.888107698459535,0.795017057233326,0.820557713571369,0.634294194129571,0.72797778020465,0.879237827530393,1,0.963182415401058", 
"Treatment.9,0.934784678841289,0.505840502482398,0.978982111839266,0.9288266084351,0.944010924367408,0.81878574572229,0.885498274066011,0.718791431723663,0.963182415401058,1"
)), class = "data.frame", row.names = c(NA, -10L)), structure(list(
    X.Treatment.1.Treatment.10.Treatment.2.Treatment.3.Treatment.4.Treatment.5.Treatment.6.Treatment.7.Treatment.8.Treatment.9 = c("Treatment.1,1,NA,NA,NA,NA,NA,NA,NA,NA,NA", 
    "Treatment.10,NA,1,NA,NA,NA,NA,NA,NA,NA,NA", "Treatment.2,NA,NA,1,NA,NA,NA,NA,NA,NA,NA", 
    "Treatment.3,NA,NA,NA,1,NA,NA,NA,NA,NA,NA", "Treatment.4,NA,NA,NA,NA,1,NA,NA,NA,NA,NA", 
    "Treatment.5,NA,NA,NA,NA,NA,1,NA,NA,NA,NA", "Treatment.6,NA,NA,NA,NA,NA,NA,1,NA,NA,NA", 
    "Treatment.7,NA,NA,NA,NA,NA,NA,NA,1,NA,NA", "Treatment.8,NA,NA,NA,NA,NA,NA,NA,NA,1,NA", 
    "Treatment.9,NA,NA,NA,NA,NA,NA,NA,NA,NA,1")), class = "data.frame", row.names = c(NA, 
-10L)))

I want a list containing all the names of the data frames. The problem is that when I write:

names(list_median)[i]

It just returns NULL. Each data frame in the list is a correlation matrix that looks like this.

CodePudding user response:

I am not understanding if this is it:

mat_names <- lapply(list_median, \(x) do.call(cbind, dimnames(x)))
mat_names <- lapply(mat_names, \(x) {colnames(x) <- c("Rows", "Cols"); x})

CodePudding user response:

Here is a possible explanation why you are running into issues. The code is commented:

# extract each dataframe to global environment with this code
for (i in seq(list_median))
  assign(paste0("df", i), list_median[[i]])

# you should see df1 and df2 etc.. in the Environment

# Now construct a list out of a few of df eg.df1 and df2 with a list of two dataframes:
my_list<- list(df1,df2)

# Now try to get the names
names(my_list)
# you will get NULL

# Now try this: name the dataframes like here and call the names:
my_list<- list(df1nownamed = df1, df2nownamed = df2)
names(my_list)

# and you will get:
[1] "df1nownamed" "df2nownamed"
  • Related