我有以下列表,其中包含多个数据帧。
> 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"))
这里我想要更改每个数据帧的第一个列名。
> 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
我正在使用以下功能
> 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
它有效但当我回想起原始列表时,列名称不会改变。
如何分配原始列表? 谢谢。
答案 0 :(得分:1)
您必须将lapply的结果分配给变量,例如
dfs <- lapply(dfs,function(x){
names(x)[1] <- 'sec'
return(x)
})