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.
You can use
rownames<-andMapas followsResult
rownames<-sets the rownames ofxtovalue. In your casexis a single dataframe inl, andvalueis a vector fromn. This function is applied "to the first elements of each ... argument, the second elements, the third elements, and so on.", from?mapply.And "
Mapis a simple wrapper tomapplywhich does not attempt to simplify the result", see?Map