1

I have the following problem:

I have a list of names from which I created dataframes for each name with the following code:

for (i in 1:nrow(list_of_names)) {
  assign(paste(list_of_names[i,1]), data.frame(0, 0))
}

list_of_names is a dataframe with character values in rows 1:9

Now, I would like to use each character in my list_of_names to get access to the respective dataframe I created.

For example I would like to change the column names in each dataframe with a loop:

for (i in 1:nrow(list_of_names)) {
  colnames(paste(list_of_names[i,1])) <- c("Date", "Value")
}

However, my current tries did not work out. Does anyone have an idea how to solve this? Or is this not possible?

Many thanks!

moellivm
  • 55
  • 3
  • 2
    [Best practice is to avoid having a bunch of data.frames not in a list.](https://stackoverflow.com/a/24376207/1422451) – Parfait Aug 02 '20 at 12:45

4 Answers4

3

Use get, set names using setNames and re-assign back:

list_of_names <- c("a","b")

col_names <- c("col1", "col2")

for (i in seq_along(list_of_names)) {
  assign(list_of_names[i], data.frame(0, 0))
}

for (i in seq_along(list_of_names)) {
  assign(list_of_names[i], setNames(get(list_of_names[i]), col_names))
}

Output

> a
  col1 col2
1    0    0
slava-kohut
  • 3,895
  • 1
  • 4
  • 21
2

Use mget to get data in a list and change their column names with lapply :

list_data <- lapply(mget(list_of_names), function(x) {
                    names(x) <- c("Date", "Value")
                    x
              })

list_data is a list of dataframes with changed column names. You can keep the data in a list as it is easier to manage and doesn't pollute your global environment but if you wish to have them as separate dataframe again you can use list2env.

list2env(list_data, .GlobalEnv)
Ronak Shah
  • 286,338
  • 16
  • 97
  • 143
1

We can use mget with tidverse approach

library(dplyr)  
library(purrr)   
mget(list_of_names) %>%
     map(~ .x %>% set_names(c('Date', 'Value'))) %>%
     list2env(.GlobalEnv)

data

df <- structure(
  list(
    group = c(1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L,
              2L, 2L, 2L),
    time = c(0L, 1L, 2L, 0L, 1L, 2L, 0L, 1L, 2L, 0L,
             1L, 2L)
  ),
  row.names = c("1", "2", "3", "4", "5", "6", "7", "8",
                "9", "10", "11", "12"),
  class = "data.frame"
)


df1 <- df
list_of_names <- c("df", "df1")
akrun
  • 674,427
  • 24
  • 381
  • 486
0

Base R solution:

# Data.frame names to be parsed: list_of_names => character vector 
list_of_names <- c("df", "df1")

# Allocate some memory for the list of data.frames: df_list => empty list
df_list <- vector("list", length(list_of_names))

# Foreach data.frame in list_of_names retreive from .GlobalEnv, 
# change it's name and store it in the list: df_list => list of data.frames
df_list <- Map(function(i){setNames(get(df_names[i]), c("Date", "Value"))},
               seq_along(df_names))

Data:

df <- structure(
  list(
    group = c(1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L,
              2L, 2L, 2L),
    time = c(0L, 1L, 2L, 0L, 1L, 2L, 0L, 1L, 2L, 0L,
             1L, 2L)
  ),
  row.names = c("1", "2", "3", "4", "5", "6", "7", "8",
                "9", "10", "11", "12"),
  class = "data.frame"
)


df1 <- df
hello_friend
  • 4,346
  • 7
  • 12