I am trying to use lapply (and want the solution with lapply) to rename columns of a data.frame located in a list, but it's returning names, not the renamed data.frames:
# define list
li <- list(u_n = data.frame(x = 1:3), r_l = data.frame(y = 4:6))
# trying to rename columns after the element of the list they're located in
li_2 <- lapply(1:length(li),
function(x,y) colnames(y[[x]]) <- names(y)[x], y = li)
However, this returns:
[[1]]
[1] "u_n"
[[2]]
[1] "r_l"
If I use the same method as the function specified in lapply individually, it does work:
li[1]
$u_n
x
1 1
2 2
3 3
colnames(li[[1]]) <- names(li)[1]
li[1]
$u_n
u_n
1 1
2 2
3 3
See Question&Answers more detail:os