首页 > 解决方案 > 是否可以 cbind 嵌套 for 循环的多次迭代?

问题描述

我有一个嵌套的 for 循环

我正在尝试这样做,以便嵌套 for 循环的多次迭代

for 循环的结尾如下所示:

代表 疯狂的
5 23
10 21
20 19
30 17
40 15
50 12

对于每次新的迭代,我希望它添加一个列,使其看起来像这样:

代表 疯狂的 疯狂2
5 23 25
10 21 22.5
20 19 20
30 17 19
40 15 17
50 12 15
out <- list()
output <- list()
i <- 1

for(patient in c("P01", "P02", "P03", "P04", "P05")){
  for(period in c("SBP", "Laser_Mean")){
    for(reps in c(5,10,20,30,40,50)){
        for(isim in 1:20){
          print(reps)
        
          db_temp <- db_s_abs_fix%>%
            filter(Patient==patient)%>%
            filter(Period==period)%>%
            group_by(Patient, Target_num, Period, Type)%>%
            sample_n(reps, replace=TRUE)
        
          last_delay <- matt_predict(db_temp)
          print(patient)
          print(period)
          print(reps)
          print(last_delay$delay)
        
          out[[i]] <- data.frame(patient=patient, period=period, reps=reps, delay=last_delay$delay, isim=isim)
          i <- i+1
      
      }

      
    }
  }
  out <- bind_rows(out)
  
    
  d_bp02 <- out%>%
    filter(period == "SBP")%>%
    dplyr::select(patient,
                  reps,
                  AV_BP = delay)
  
  d_laser02 <- out%>%
    filter(period == "Laser_Mean")%>%
    dplyr::select(patient,
                  reps,
                  AV_Laser = delay)
  
  d_final02 <- full_join(d_bp02, d_laser02)%>%
    group_by(reps)%>%
    mutate(av_diff = AV_Laser - AV_BP,
           abs_av_diff = abs(av_diff))
  
  d_final_mad <- d_final02%>%
    group_by(reps)%>%
    summarise(med_av_diff = median(av_diff),
              med_abs_av_diff = median(abs(av_diff)),
              MAD = median(abs(av_diff - med_av_diff)))
  
  d_final_mad <- d_final_mad%>%
    group_by(reps)%>%
    dplyr::select(reps,MAD)
  
  output[[i]] <- d_final_mad
  i <- i+1
  
}

  output <- do.call(cbind,output)

我努力了:

  output <- do.call(cbind,output)

stats <- foreach(i = 1:5, .combine=data.frame) %do% {
  output(i)
}

do.call(cbind, lapply(output, as.data.frame))

以上不起作用

for (i in 1:5) {
  d_final_mad$i <- i  #to keep track of which iteration produced it
  output[[i]] <- d_final_mad # add it to your list
}

这个只是给了我5次相同的结果

标签: rfor-loopnested-loopsdata-manipulation

解决方案


您的问题的标题询问如何将额外的数据列绑定到现有数据集,因此新数据的确切派生方式无关紧要。像@Elin 一样,我对您的工作流程感到非常困惑,所以我将忽略它,并简单地向您展示如何绑定新列。

首先,cbind在现有代码中使用 as。

d <- tibble(reps=c(5, 10, 20, 30, 40, 50), MAD=c(23, 21, 19, 17, 15, 12))
# Define the data to be added in any way you like
newData <- c(25, 22.5, 20, 19, 17, 15)
# How many MAD* columns exist already?
madCount <- sum(str_count(names(d), "MAD\\d*"))
# Derive the new column names
colNames <- c(names(d), paste0("MAD", madCount + 1))
# Bind the new data
d <- d %>% cbind(newData)
names(d) <- colNames
d
  reps MAD MAD2
1    5  23 25.0
2   10  21 22.5
3   20  19 20.0
4   30  17 19.0
5   40  15 17.0
6   50  12 15.0

再做一次测试

madCount <- sum(str_count(names(d), "MAD\\d*"))
colNames <- c(names(d), paste0("MAD", madCount + 1))
d <- d %>% cbind(newData)
names(d) <- colNames
d
  reps MAD MAD2 MAD3
1    5  23 25.0 25.0
2   10  21 22.5 22.5
3   20  19 20.0 20.0
4   30  17 19.0 19.0
5   40  15 17.0 17.0
6   50  12 15.0 15.0

现在使用 tidyverse。

d <- tibble(reps=c(5, 10, 20, 30, 40, 50), MAD=c(23, 21, 19, 17, 15,
# How many MAD* columns exist already?
madCount <- sum(str_count(names(d), "MAD\\d*"))
# Derive the new column names
colName <- paste0("MAD", madCount + 1)
# Bind the new data
d <- d %>% bind_cols({{colName}}:=newData)
d
# A tibble: 6 x 3
   reps   MAD  MAD2
  <dbl> <dbl> <dbl>
1     5    23  25  
2    10    21  22.5
3    20    19  20  
4    30    17  19  
5    40    15  17  
6    50    12  15  

再次检查它是否适用于更多列。

madCount <- sum(str_count(names(d), "MAD\\d*"))
colName <- paste0("MAD", madCount + 1)
d <- d %>% bind_cols({{colName}}:=newData)
d 
# A tibble: 6 x 4
   reps   MAD  MAD2  MAD3
  <dbl> <dbl> <dbl> <dbl>
1     5    23  25    25  
2    10    21  22.5  22.5
3    20    19  20    20  
4    30    17  19    19  
5    40    15  17    17  
6    50    12  15    15  

推荐阅读