mercredi 25 août 2021

Add data to existing dataframe based on condition in R

I have a large number of csv file.(more than 500csv file) Currently I have to extract the specific cells 1 by 1 csv manually.And make a new dataframe based on category. Below is my code to import the data, create variable for every csv specific cells information and create a new dataframe.

#Import the csv and create a list
tempx <- list.files(pattern ="*.csv")
mylist <- lapply(tempx,read_csv)

Create variable one by one for extract specific data in every csv.file and delete the zero value

file1 <- mylist[[1]][132:167,4][apply(mylist[[1]][132:167,4], 1,
                                          function(row) all(row !=0 )), ] 


file2 <- mylist[[2][132:167,4][apply(mylist[[2][132:167,4], 1,
                                          function(row) all(row !=0 )), ] 

file3 <- mylist[[3]][132:167,4][apply(mylist[[3]][132:167,4], 1,
                                          function(row) all(row !=0 )), ]

file4 <- mylist[[3]][132:167,4][apply(mylist[[3]][132:167,4], 1,
                                          function(row) all(row !=0 )), ]

file5 <- mylist[[3]][132:167,4][apply(mylist[[3]][132:167,4], 1,
                                          function(row) all(row !=0 )), ]

Create a unequal length dataframe.


table <- qpcR:::cbind.na((file1,file3,file4,file5)
                                    

colnames(table) <- c("Name1" ,"Name2")

table1 <- as.data.frame(lapply(table ,as.numeric))  

Above code is the one i can only do.Manually create those variablefile1 to file500 and slot in variable 1by1 to the table dataframe column based on the row17,column 4 of that csv file. (example of file105: my list[[105]][17,4].)

Is it possible to use if else or case_when to add row to table1(existing dataframe or create a new dataframe) based on mylist[[1]][17,4] or [[2]][17,4] until [[500]][17,4] etc. (finally the dataframe will be 10+ Column for all the 500 csv file.)

Just like if[[1]][17,4]="Name1", slot in the [[1]][132:167,4] without zero value to table column "Name1".

Aucun commentaire:

Enregistrer un commentaire