Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an empty list?

Tags:

list

r

Here is a code (also see comments in code):

library(rlist)

lwa_res_lst <- vector(mode = "list", length = 1) # Create empty list. Unfortunatly with this method I cannot create list with 0 length.
current_res <- 1
  
lwa_res_lst <- list.append(lwa_res_lst, current_res)  # Append the element at the end of the list.

My question is: How to initially create empty list to which after can be appended an element with list.append() at position 1?

like image 305
vasili111 Avatar asked Oct 23 '25 17:10

vasili111


2 Answers

We can just do assignment

lwa_res_list[[1]] <- current_res

In the OP's code, if the list was initialized as

lwa_res_lst <- list()

Or using OP's code

lwa_res_lst <- vector(mode = "list", length = 0)

the code should work

lwa_res_lst <- list.append(lwa_res_lst, current_res) 
lwa_res_lst
#[[1]]
#[1] 1
like image 161
akrun Avatar answered Oct 26 '25 07:10

akrun


current_res <- 1  #any number you want for list

lwa_res_lst <- vector(mode = "list", length = current_res)

lwa_res_lst


class(lwa_res_lst)
like image 23
Abdullah Alotaibi Avatar answered Oct 26 '25 08:10

Abdullah Alotaibi



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!