0

I have the following list with multiple dataframes .

> dput(dfs)
structure(list(a = structure(list(x = 1:4, a = c(0.114304427057505, 
0.202305722748861, 0.247671527322382, 0.897279736353084)), .Names = c("x", 
"a"), row.names = c(NA, -4L), class = "data.frame"), b = structure(list(
    x = 1:3, b = c(0.982652948237956, 0.694535500137135, 0.0617770322132856
    )), .Names = c("x", "b"), row.names = c(NA, -3L), class = "data.frame"), 
    c = structure(list(x = 1:2, c = c(0.792271690675989, 0.997932326048613
    )), .Names = c("x", "c"), row.names = c(NA, -2L), class = "data.frame")), .Names = c("a", 
"b", "c"))

here i want change the first column name of each dataframe.

> dfs
$a
  x         a
1 1 0.1143044
2 2 0.2023057
3 3 0.2476715
4 4 0.8972797

$b
  x          b
1 1 0.98265295
2 2 0.69453550
3 3 0.06177703

$c
  x         c
1 1 0.7922717
2 2 0.9979323

I am using the following function

> lapply(dfs,function(x){ names(x)[1] <- 'sec';x})
$a
  sec         a
1   1 0.1143044
2   2 0.2023057
3   3 0.2476715
4   4 0.8972797

$b
  sec          b
1   1 0.98265295
2   2 0.69453550
3   3 0.06177703

$c
  sec         c
1   1 0.7922717
2   2 0.9979323

It's works but when i recall the original list ,the column names are not change.

How to assign to original list? Thank you.

dondapati
  • 829
  • 6
  • 18

1 Answers1

2

You have to assign the result of lapply to a variable, like this

dfs <- lapply(dfs,function(x){ 
    names(x)[1] <- 'sec'
    return(x)
})
Linus
  • 705
  • 1
  • 10
  • 20