Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
685 views
in Technique[技术] by (71.8m points)

matrix - R: How do you use ncol() dynamically in lapply?

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

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

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...