Search code examples
rrep

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


I have this vector (10x fold dilutions in replicates)

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

and want to adjust it based on the length of each column in the df, e.g. if CT_1 has 15 values, then the amount should be amount<-rep(c(0.1, 0.01, 0.001, 0.0001, 0.00001), each = 3)

if CT3 HAS 20 values, then the amount should be

amount<-rep(c(0.1, 0.01, 0.001, 0.0001, 0.00001, 0.000001, 0.0000001), each = 3) Since there is only 2 replicates for the amount 0.0000001 in CT_3, would it be a problem that each = 3 in amount?

but is there a way to control the size of rep for every different case (=the varying number of rows of any of the columns)?

    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 

Solution

  • You can just simply get only n elements from the rep

    
    # 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
      # ...
    }