user2380782
user2380782

Reputation: 1446

replace a value in a dataframe in a list of data.frames and save each output to a new list

I have a list of data.frames with several columns and different number of rows, I would like to create a new set of lists when the values of Cumulative column will be zero but recursively, i.e, one output with the first value of Cumulative column being zero, another output with the second value being zero and so on.

Here is how my list looks like:

$test
$ABO
   Locus   Gene   Link Semantic Combined Cumulative Notes
1    ABO GTF3C5 0.0173   0.2622   0.1398     0.1398  Good
2    ABO  GBGT1 0.0734   0.0790   0.0762     0.2160      
3    ABO    ABO 0.1363   0.0125   0.0744     0.2904      
4    ABO   BRD3 0.0006   0.1048   0.0527     0.3431      
5    ABO   VAV2 0.0020   0.1023   0.0521     0.3952      
6    ABO   WDR5 0.0546   0.0441   0.0494     0.4446      
7    ABO  RPL7A 0.0345   0.0640   0.0493     0.4939      


$ACSL1
   Locus     Gene   Link Semantic Combined Cumulative Notes
37 ACSL1    CENPU 0.3358   0.0000   0.1679     0.1679  Good
38 ACSL1  CCDC110 0.0175   0.2900   0.1538     0.3217      
39 ACSL1 TRAPPC11 0.0143   0.2658   0.1400     0.4617      
40 ACSL1     IRF2 0.0316   0.2063   0.1189     0.5806      
41 ACSL1  ANKRD37 0.0707   0.1001   0.0854     0.6660      
42 ACSL1    ACSL1 0.0333   0.1157   0.0745     0.7405      

$ADAMTS9
     Locus    Gene   Link Semantic Combined Cumulative Notes
56 ADAMTS9   ATXN7 0.1424        1   0.5712     0.5712  Good
57 ADAMTS9 C3orf49 0.3463        0   0.1732     0.7444      

$ADCY5
   Locus   Gene   Link Semantic Combined Cumulative Notes
62 ADCY5  ADCY5 0.4132   0.8535   0.6333     0.6333  Good
63 ADCY5 SEMA5B 0.1679   0.0000   0.0839     0.7172    

$ANK1
  Locus   Gene   Link Semantic Combined Cumulative Notes
75  ANK1   POLB 0.0820   0.2270   0.1545     0.1545  Good    
76  ANK1 NKX6-3 0.2563   0.0000   0.1282     0.2827      
77  ANK1   ANK1 0.1314   0.0690   0.1002     0.3829      
78  ANK1  SFRP1 0.0628   0.1206   0.0917     0.4746      
79  ANK1  AP3M2 0.1118   0.0626   0.0872     0.5618      
80  ANK1   PLAT 0.1554   0.0000   0.0777     0.6395      
81  ANK1 CHRNA6 0.0031   0.1418   0.0725     0.7120   

I have been trying to implement a for loop but I only get one list and the last value of the Cumulative column being zero. I need to do downstream analysis with each of the output lists Here is my code so far:

list.1 <- list()
for (i in 1:length(test)){
    for (j in 1:dim(scores.thres.v2[[i]])[1]){
        df <- scores.thres.v2[[i]]
        df[j,"Cumulative"] <- 0
        list.1[[i]] <- df
    }
}

Thanks

Upvotes: 0

Views: 84

Answers (1)

lmo
lmo

Reputation: 38520

My interpretation of your question: loop through each data.frame in a list. Within the loop, loop through each row and return a data.frame where the variable "Cumulative" is 0. The end result is a list of a list of data frames.

Here is one method to accomplish this:

myList <- lapply(test, function(i) lapply(1:nrow(i),
                                          function(j) {i[j, "Cumulative"] <- 0; i}))

data

ABO <- read.table(header=T, , stringsAsFactors=F, text="Locus   Gene   Link Semantic Combined Cumulative
1    ABO GTF3C5 0.0173   0.2622   0.1398     0.1398
2    ABO  GBGT1 0.0734   0.0790   0.0762     0.2160      
3    ABO    ABO 0.1363   0.0125   0.0744     0.2904      
4    ABO   BRD3 0.0006   0.1048   0.0527     0.3431      
5    ABO   VAV2 0.0020   0.1023   0.0521     0.3952      
6    ABO   WDR5 0.0546   0.0441   0.0494     0.4446      
7    ABO  RPL7A 0.0345   0.0640   0.0493     0.4939")

ACSL1 <- read.table(header=T, , stringsAsFactors=F, text="Locus     Gene   Link Semantic Combined Cumulative
37 ACSL1    CENPU 0.3358   0.0000   0.1679     0.1679
38 ACSL1  CCDC110 0.0175   0.2900   0.1538     0.3217      
39 ACSL1 TRAPPC11 0.0143   0.2658   0.1400     0.4617      
40 ACSL1     IRF2 0.0316   0.2063   0.1189     0.5806      
41 ACSL1  ANKRD37 0.0707   0.1001   0.0854     0.6660      
42 ACSL1    ACSL1 0.0333   0.1157   0.0745     0.7405")

test <- list("ABO"=ABO, "ACSL1"=ACSL1)

Upvotes: 1

Related Questions