samedi 27 avril 2019

Lapply to a list of dataframes only if column exists

I have a list of dataframes for which I want to obtain (in a separate dataframe) the row mean of a specified column which may or may not exist in all dataframes of the list. My problem comes when the specified column does not exist in at least one of the dataframes of the list.

Assume the following example list of dataframes:

df1 <- read.table(text = 'X   A   B   C
                       name1  1   2   3
                       name2  5  10   4',
                 header = TRUE)  

df2 <- read.table(text = 'X   B   C   A
                       name1  8   1  31
                       name2  9   9   8', 
                 header = TRUE)

df3 <- read.table(text = 'X   B   A   E
                       name1  9   9  29
                       name2  5  15  55', 
                 header = TRUE)

mylist_old <-list(df1, df2)
mylist_new <-list(df1, df2, df3)

Assume I want to rowMeans column C the following piece of code works perfectly when the list of dataframe (mylist_old) is composed of elements df1 and df2, :

Mean_C <- rowMeans(do.call(cbind, lapply(mylist_old, "[", "C")))
Mean_C <- as.data.frame(Mean_C)

The trouble comes when the list is composed of at least one dataframe for which column C does not exist, which in my example is the case of df3, that is for list mylist_new:

Mean_C <- rowMeans(do.call(cbind, lapply(mylist_new, "[", "C")))

Leads to: "Error in [.data.frame(X[[i]], ...) : undefined columns selected

One way to circumvent this issue is to exclude df3 from mylist_new. However, my real program has a list of 64 dataframes for which I do not know whether column C exists or not. I would have like to lapply my piece of code only if column C is detected as existing, that is applying the command to the list of dataframes but only for dataframes for which existence of column C is true.

I tried this

if("C" %in% colnames(mylist_new))
 {
     Mean_C <- rowMeans(do.call(cbind, lapply(mylist_new, "[", "C")))
     Mean_C <- as.data.frame(Mean_C)    
 }

But nothing happens, probably because colnames refers to the list and not to each dataframe of the list. With 64 dataframes, I cannot refer to each "manually" and need an automated procedure.

Aucun commentaire:

Enregistrer un commentaire