【问题标题】:How to create an empty list?如何创建一个空列表?
【发布时间】:2021-05-04 17:24:54
【问题描述】:

这是一个代码(另见代码中的 cmets):

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.

我的问题是:如何最初创建一个空列表,然后可以在位置1 处附加一个带有list.append() 的元素?

【问题讨论】:

  • 这个函数只是c:c(list(), 1)或c(NULL, list(1))会给你一样的

标签: r list


【解决方案1】:

我们可以做任务

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

【讨论】:

    【解决方案2】:
    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)
    

    【讨论】:

      猜你喜欢
      • 2019-05-30
      • 1970-01-01
      • 2011-02-27
      • 2021-03-14
      • 1970-01-01
      • 1970-01-01
      • 2022-07-12
      • 2017-12-03
      • 2022-10-04
      相关资源
      最近更新 更多