我有一个for循环,它提取等长的数字范围,并将每个范围作为列表中的一个新列表追加.在for循环完成后,我希望将列表列表转换为数据框并计算行平均值.我目前不能将列表列表转换为Tibble,因为单独的列表没有命名.
如何将每个新列表命名为与循环迭代关联的整数?(例如,1、2、3……)
下面是简化的示例.我的Actual for循环做了一些更复杂的事情,但它捕捉到了我遇到的问题.
谢谢!
library(tidyverse)
input_df <- data.frame(
Name = c("A", "B", "C"),
Start = c(100,250,350),
Stop = c(150,300,400)
)
All_list <- list()
for(i in 1:length(input_df$Name)) {
myoutput <- data.frame(
Value=seq.int(paste0(input_df$Start[i]), paste0(input_df$Stop[i]))
)
#How do I specify that the name of added list is the value of the iteration [i]?
All_list[[i]] <- myoutput$Value
}
All_df <- All_list %>%
as_tibble() %>%
rowwise() %>%
dplyr::mutate(Average = mean(c_across(1:length(myvector)))) %>%
ungroup()
Error in `as_tibble()`:
! Columns 1, 2, and 3 must be named.
Use `.name_repair` to specify repair.