Home > OS >  Joining 'n' number of lists and perform a function in R
Joining 'n' number of lists and perform a function in R

Time:07-13

I have a dataframe which contains many triplicate (3 columns set). And I have grouped the dataframe into each triplicate as a seperate group of list.

The example dataset is,

example_data <- structure(list(`1_3ng` = c(69648445400, 73518145600, NA, NA, 
73529102400, 75481088000, NA, 73545910600, 74473949200, 77396199900
), `2_3ng` = c(71187990600, 70677690400, NA, 73675407400, 73215342700, 
NA, NA, 69996254800, 69795686400, 76951318300), `3_3ng` = c(65032022000, 
71248214000, NA, 72393058300, 72025550900, 71041067000, 73604692000, 
NA, 73324202000, 75969608700), `4_7-5ng` = c(NA, 65845061600, 
75009245100, 64021237700, 66960666600, 69055643600, NA, 64899540900, 
NA, NA), `5_7-5ng` = c(65097201700, NA, NA, 69032126500, NA, 
70189899800, NA, 74143529100, 69299087400, NA), `6_7-5ng` = c(71964413900, 
69048485800, NA, 71281569700, 71167596500, NA, NA, 68389822800, 
69322289200, NA), `7_10ng` = c(71420403700, 67552276500, 72888076300, 
66491357100, NA, 68165019600, 70876631000, NA, 69174190100, 63782945300
), `8_10ng` = c(NA, 71179401200, 68959365100, 70570182700, 73032738800, 
NA, 74807496700, NA, 71812102100, 73855098500), `9_10ng` = c(NA, 
70403756100, NA, 70277421000, 69887731700, 69818871800, NA, 71353886700, 
NA, 74115466700), `10_15ng` = c(NA, NA, 68487581700, NA, NA, 
69056997400, NA, 67780479400, 66804467800, 72291939500), `11_15ng` = c(NA, 
63599643700, NA, NA, 60752029700, NA, NA, 63403655600, NA, 64548492900
), `12_15ng` = c(NA, 67344750600, 61610182700, 67414425600, 65946654700, 
66166118400, NA, 70830837700, 67288305700, 69911451300)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -10L)

And after grouping I got the four lists, since the above example dataset contains 4 groups. I have used the following R code for grouping the data,

grouping_data<-function(df){                    #df= dataframe
df_col<-ncol(df)                                #calculates no. of columns in dataframe
groups<-sort(rep(0:((df_col/3)-1),3))           #creates user determined groups
id<-list()                                      #creates empty list
for (i in 1:length(unique(groups))){
  id[[i]]<-which(groups == unique(groups)[i])}  #creates list of groups
names(id)<-paste0("id",unique(groups))          #assigns group based names to the list "id"
data<-list()                                    #creates empty list
for (i in 1:length(id)){
  data[[i]]<-df[,id[[i]]]}                      #creates list of dataframe columns sorted by groups
names(data)<-paste0("data",unique(groups))      #assigns group based names to the list "data"
return(data)}
group_data <-grouping_data(example_data)
  

Please suggest useful R code for do a particular function for all the lists at a same time.

For example the below function I have done by following way,

     #VSN Normalization
      vsnNorm <- function(dat) {
        dat<-as.data.frame(dat)
        vsnNormed <- suppressMessages(vsn::justvsn(as.matrix(dat)))
        colnames(vsnNormed) <- colnames(dat)
        row.names(vsnNormed) <- rownames(dat)
        return(as.matrix(vsnNormed))
      }

And I have tried like below,

      vsn.dat0 <- vsnNorm(group_data$data0)
      vsn.dat1 <- vsnNorm(group_data$data1)
      vsn.dat2 <- vsnNorm(group_data$data2)
      vsn.dat3 <- vsnNorm(group_data$data3)
      vsn.dat <- cbind (vsn.dat0,vsn.dat1,vsn.dat2,vsn.dat3)

It is working well. But the dataset triplicate (3 columns set) value may be change from dataset to dataset. And calling all the lists everytime become will be tedious.

So kindly share some codes which will call all the resulted lists for performing a function and combine the result as a single file.

Thank you in advance.

CodePudding user response:

The shortcut you are looking for is:

vsn.dat <- do.call("rbind", lapply(group_data, vsnNorm))
  • Related