这是一个代码(另请参阅代码中的注释):
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()
附加一个元素>?
最佳答案
我们可以只做作业
lwa_res_list[[1]] <- current_res
在OP的代码中,如果列表
被初始化为
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
关于r - 如何创建一个空列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67389392/