rdataframefor-loopr-rownames

100x loop, get rownames from dataframe A and add as a column to a new dataframe B each time, ending with 100 columns in B


I want to execute a 100x , get rownames from a A and add its rownames as values in a column to a new B each time, ending with 100 columns in B. I am beating my head against the wall here trying to do this, as I seem to overwrite B every way I have tried it.

     data <- bs.exp.sorted.1198.input
     outsft = dataframe
     outsamps = dataframe 
     samples<- data.frame(matrix(0, nrow = 60, ncol = 1))
     currentsamps =dataframe 
     itnumb=100 

    for(i in 1:itnumb){
    subsamp <- data[sample(nrow(data), 60), ]

    powers =c(c(1:10), seq(from=12,to=20,by=2))
    sft =pickSoftThreshold(bs.exp.sorted.1198.input, powerVector = powers, verbose=5)


    outsft <- rbind(outsft, sft$fitIndices[,2])

    currentsamps <- as.data.frame(rownames(subsamp))
    output <- add_column(samples, currentsamps)

    }

output always contains a column of 0s (which I had to include to get it to work... And whatever the last set of rownames were in the loop. I want to save ALL the rownames in one big dataframe. This should be a simple task, but it has me stumped, and at this point, infuriated. Any suggestions? Thanks.


Solution

  • How about this:

    initial_rows = 100
    initial_df = data.frame( col1 = rnorm( initial_rows ),  
                             row.names = 1:initial_rows )
    stores_names = rownames( initial_df )
    
    chunk_size = 10
    n_repetitions = 10
    result = data.frame( matrix( NA_character_, 
                                 nrow = chunk_size, ncol = n_repetitions ) )
    
    for ( i_rep in seq_len(n_repetitions) ) {
    
      current_names = sample( stores_names, chunk_size ) 
      result[ , i_rep ] = current_names
    
    }
    
    > result
       X1  X2 X3 X4 X5 X6 X7 X8 X9 X10
    1  72  62 24 35 80 48  6 79 17  96
    2  66   4 59 39 32 67 95 96 89  26
    3  34  21 96 75 22 87  8 49 98  50
    4  40  33 74 90 39  2 86 78 15   3
    5  87  12 70 72 62 98 69  4  1  32
    6  98  43  7 38 18 57 83 47 35  41
    7  28  70  6 84 30  4 54 46  4  35
    8  94 100 80 36 84  7 56 36 73   9
    9  60  88 28 11 73 32  3 23 85  67
    10 69  69 68 69  4 17 60 63 62  47
    

    Is this what you wanted?