I have list of dataframes
that I want to rename such that the word Existing
appears in the name of each of the dataframe.
How can I do this without having to manually add the new name of each dataframe (my actual list has 10 dataframes)?
Sample Data list:
df_list= list(a=1:5, b=2:10, c=-5:5)
Desired output:
df_list = list(Existing, Existing_1, Existing_3) # Something similar
Code:
library(tidyverse)
library(dplyr)
# Rename dfs in the list
names(df_list) = "Existing" #...stuck
CodePudding user response:
names(df_list) <- paste0('Existing_', seq_along(df_list))
df_list
$Existing_1
[1] 1 2 3 4 5
$Existing_2
[1] 2 3 4 5 6 7 8 9 10
$Existing_3
[1] -5 -4 -3 -2 -1 0 1 2 3 4 5
CodePudding user response:
1) Use c(...) like this:
c(Existing = df_list)
giving:
$Existing.a
[1] 1 2 3 4 5
$Existing.b
[1] 2 3 4 5 6 7 8 9 10
$Existing.c
[1] -5 -4 -3 -2 -1 0 1 2 3 4 5
2) If we remove the names first then we get this:
c(Existing_ = setNames(df_list, NULL))
giving:
$Existing_1
[1] 1 2 3 4 5
$Existing_2
[1] 2 3 4 5 6 7 8 9 10
$Existing_3
[1] -5 -4 -3 -2 -1 0 1 2 3 4 5