簡體   English   中英

如何創建一個空列表?

[英]How to create an empty list?

這是一個代碼(另見代碼中的注釋):

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.

我的問題是:如何最初創建一個空列表,之后可以在 position 1處附加一個帶有list.append()的元素?

我們可以做任務

lwa_res_list[[1]] <- current_res

在 OP 的代碼中,如果list被初始化為

lwa_res_lst <- list()

或使用 OP 的代碼

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

代碼應該可以工作

lwa_res_lst <- list.append(lwa_res_lst, current_res) 
lwa_res_lst
#[[1]]
#[1] 1
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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM