如何根据 df 中特定列的 nrows 减少代表的长度

How to decrease the length of a rep based on the nrows of specific columns in df

我有这个载体(重复 10 倍稀释)

amount<-rep(c(0.1, 0.01, 0.001, 0.0001, 0.00001, 0.000001, 0.0000001), each = 3)

并希望根据 df 中每列的长度进行调整,例如如果 CT_1 有 15 个值,那么数量应该是 amount<-rep(c(0.1, 0.01, 0.001, 0.0001, 0.00001), each = 3)

如果CT3有20个值,那么数量应该是

amount<-rep(c(0.1, 0.01, 0.001, 0.0001, 0.00001, 0.000001, 0.0000001), each = 3) 由于 CT_3 中的数量 0.0000001 只有 2 个重复,数量 each = 3 会不会有问题?

但是有没有办法控制每种不同情况下的代表大小(=任何列的不同行数)?

    CT_1  CT_2  CT_3  amount
   <dbl> <dbl> <dbl>   <dbl>
 1  14.2  12.4  12.0 1      
 2  14.2  12.4  11.9 1      
 3  14.3  12.8  11.8 1      
 4  17.4  16.5  15.9 0.1    
 5  17.3  16.5  15.9 0.1    
 6  17.4  16.6  15.6 0.1    
 7  21.2  19.9  19.3 0.01   
 8  21.3  20.0  19.6 0.01   
 9  21.1  20.0  19.3 0.01   
10  24.6  23.2  20.4 0.001  
11  24.6  23.5  22.0 0.001  
12  24.8  23.4  22.8 0.001  
13  27.6  26.4  26.1 0.0001 
14  27.8  26.5  26.1 0.0001 
15  27.8  26.5  26.1 0.0001 
16  NA    30.0  29.3 0.00001
17  NA    30.2  29.5 0.00001
18  NA    30.4  NA   0.00001 

您可以只从 rep

中获取 n 个元素

# or check if number of not NA in your data
for (current_col in names(data)) {
  col_data <- data[[current_col]]
  
  col_data <- col_data[!is.na(col_data)]
  # get number of row in your data
  n_max <- length(col_data)
  
  # Use length.out for control the size of rep
  amount <- rep(c(0.1, 0.01, 0.001, 0.0001, 0.00001,  0.000001, 0.0000001),
    each = 3, length.out = n_max)
  
  # Your regression analysis code here
  # ...
}