'How to replace rownames of a list of dataframes with values from a list of character strings in R?

Let l be a list of two dataframes and n a list of character strings, wherein l and n both have two elements. Also, let the number of rows of each dataframe in l be equal to the length of the vector of character strings in n.

l <- list(data.frame(x = c(1,2,3), y = c(1,2,3)),
          data.frame(x = c(1,2,3,4), y = c(1,2,3,4)))
n <- list(c('a', 'b', 'c'), c('a', 'b', 'c', 'd'))

Suppose we want to replace the rownames of the two dataframes in l with the character strings in n. The desired result would be:

> l
[[1]]
  x y
a 1 1
b 2 2
c 3 3

[[2]]
  x y
a 1 1
b 2 2
c 3 3
d 4 4

I believe one could exploit mapply for this task, but I cannot figure how to do this.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source