I have these data frames in R within the global environment:
file_1 <- data.frame(id = 1:5, samplecolumn = letters[1:5], value = rnorm(5))
file_2 <- data.frame(id = 1:5, sample_column = letters[6:10], value = rnorm(5))
file_3 <- data.frame(id = 1:5, othercolumn = letters[11:15], value = rnorm(5))
abc_1 <- data.frame(id = 1:5, samplecolumn = letters[16:20], value = rnorm(5))
For all data frames starting with "file_", if there is a column the name "samplecolumn", I want to rename it as "sample_column".
I thought that I could do this by first identifying all data frames starting with "file_", and then renaming them:
all_objects <- ls()
file_objects <- all_objects[grep("^file_", all_objects)]
for (file in file_objects) {
df <- get(file)
if ("samplecolumn" %in% colnames(df)) {
colnames(df)[colnames(df) == "samplecolumn"] <- "sample_column"
assign(file, df)
}
}
Is this the correct way to do this in R?
Here is a base R way. Get the data frames with ls/mget
, then lapply
a function making the changes.
ls(pattern = "file_") |>
mget() |>
lapply(\(x) {
names(x) <- ifelse(grepl("samplecolumn", names(x)), "sample_column", names(x))
x
})
#> $file_1
#> id sample_column value
#> 1 1 a 0.3567935
#> 2 2 b 0.1467364
#> 3 3 c 0.2088358
#> 4 4 d 1.9435469
#> 5 5 e -1.6413247
#>
#> $file_2
#> id sample_column value
#> 1 1 f -0.2842375
#> 2 2 g 0.5040112
#> 3 3 h 2.5262186
#> 4 4 i 0.5083768
#> 5 5 j 0.7501698
#>
#> $file_3
#> id othercolumn value
#> 1 1 k -0.17416173
#> 2 2 l -0.04321274
#> 3 3 m -0.16325477
#> 4 4 n -0.53594218
#> 5 5 o 1.11494155
Created on 2024-09-28 with reprex v2.1.0
Or, more simple, with setNames
.
ls(pattern = "file_") |>
mget() |>
lapply(\(x)
setNames(x, ifelse(grepl("samplecolumn", names(x)), "sample_column", names(x)))
)
#> $file_1
#> id sample_column value
#> 1 1 a 1.5820405
#> 2 2 b -0.6436687
#> 3 3 c -0.5669997
#> 4 4 d 0.9622013
#> 5 5 e -0.9325018
#>
#> $file_2
#> id sample_column value
#> 1 1 f -0.3382037
#> 2 2 g 0.3404972
#> 3 3 h 0.2350941
#> 4 4 i -0.1942233
#> 5 5 j 0.7478564
#>
#> $file_3
#> id othercolumn value
#> 1 1 k 2.3387201
#> 2 2 l -1.0179311
#> 3 3 m 0.3301583
#> 4 4 n -0.3459007
#> 5 5 o 2.2805970
Created on 2024-09-28 with reprex v2.1.0