Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to predefine the length of a list

Tags:

r

How can I create a list of size n? For example, lines = read.table(filename, sep="\n") how would I create a list called linesCopy that is of the size of lines?

I tried something like linesCopy <- [[length(lines)]] but this throws unexpected token [[

like image 970
Apollo Avatar asked Dec 18 '25 04:12

Apollo


1 Answers

A list is just a vector, you can create a list of any size using vector().

For example, a list of size 4:

vector("list", 4)
[[1]]
NULL

[[2]]
NULL

[[3]]
NULL

[[4]]
NULL

Or, specific in your case, a list named linesCopy with the same size as the length of lines:

linesCopy <- vector("list", length(lines))
like image 58
Carlos Cinelli Avatar answered Dec 20 '25 17:12

Carlos Cinelli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!