I am trying to add a row NewRow
to each matrix in a list mylist
. NewRow
should be a vector of integers from 1 until the end of the row. Each matrix in mylist
has a different number of columns, so the last number in the NewRow
vector isn't constant, it has to be dynamic. These are my attempts:
mylist.with.new.row <- lapply(mylist, rbind, NewRow = 1:ncol())
mylist.with.new.row <- lapply(mylist, rbind, NewRow = 1:ncol(mylist))
Neither work because ncol()
can't be empty, but also can't just be ncol(mylist)
because it has to be dynamic.
I assume this can be done with a loop but I'm hoping someone knows a quicker way.
Thanks!
question from:
https://stackoverflow.com/questions/65850066/r-how-do-you-use-ncol-dynamically-in-lapply 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…