Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
697 views
Welcome To Ask or Share your Answers For Others

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...