Home > other >  How can I change rownames of dataframes in a list adding their own list names in R?
How can I change rownames of dataframes in a list adding their own list names in R?

Time:01-16

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.

CodePudding user response:

An approach using mapply

mapply(function(lis, nm){rownames(lis) <- paste0(nm, ".", rownames(lis))
    list(lis)},
  dlis, names(dlis))
$a
    col
a.k   1
a.l   2
a.m   3

$b
    col
b.o   4
b.p   5
b.r   6

Data

dlis <- list(a = structure(list(col = c(1, 2, 3)), class = "data.frame", row.names = c("k",
"l", "m")), b = structure(list(col = c(4, 5, 6)), class = "data.frame", row.names = c("o",
"p", "r")))

CodePudding user response:

Instead of iterating over the list, we can iterate over the name of the list, which can have better control when pasting the names together.

This is modified from your attempt:

setNames(lapply(names(l), 
                \(x) l[[x]] %>% `row.names<-` (paste(x, rownames(l[[x]]), sep = "."))), 
         names(l))

$a
    col
a.k   1
a.l   2
a.m   3

$b
    col
b.o   4
b.p   5
b.r   6

CodePudding user response:

1) rbind Use rbind to create a single data frame with the desired row names and then split it back to the original.

split(do.call("rbind", l), rep(names(l), sapply(l, nrow)))

2) data.frame Use data.frame and its row.names= argument to avoid row.names<-

rnames <- function(x, nm) data.frame(x, row.names = paste0(nm, ".", rownames(x)))
Map(rnames, l, names(l))

3) for Use a for loop creating ll

ll <- l
for(nm in names(ll)) rownames(ll[[nm]]) <- paste0(nm, ".", rownames(ll[[nm]]))
  • Related