I try to change row.names
of each dataframe in a list by adding their names to rownames.
List
is:
l <- list(a=data.frame(col = c(1,2,3),row.names = c("k","l","m")), b=data.frame(col =
c(4,5,6), row.names = c("o","p","r")))
I tried this but couldn't add index names a
and b
:
lapply(l,function(x) {x %>% `row.names<-` (paste(names(l)[which(l %in%
x)],rownames(x),sep = "."))})
l$a
is:
row.names | col |
---|---|
k | 1 |
l | 2 |
m | 3 |
But it should be like this:
row.names | col |
---|---|
a.k | 1 |
a.l | 2 |
a.m | 3 |
What should I do? Thank you in advance.