I am total newby to R and want to apply a specific function to a list of data.frames.
The dataframes contain rows completely filled with 0 and I want to delete them from my dataframe.
Sample dataframe:
| OTU ID | Sample 1 | Sample 2 | Sample 3 | Sample 4 |
| :--- | :--- | :--- | :--- | :--- |
| abc | 12 | 24 | 0 | 120 |
| bcd | 0 | 0 | 0 | 0 |
| efg | 12 | 24 | 0 | 120 |
| hij | 24 | 9 | 13 | 4 |
For one table the code would be as follows:
#' in column 1 are the rownames, so the rowSums-function should be applied to all columns besides column 1
all_zero <- rowSums(table1[,-1]) == 0
#' then the rows that include only 0 should be deleted from the data.frame
table1 <- filter(table1, !all_zero)
As I have 10 different data.frames on which I want to apply the function, I want to create a for-loop or lapply()
#' first I created a list of the data.frames
all_df <- mget(ls([1:10])
and then I get stuck. Maybe you can help me finalize the options
a) for-loop (Here maybe it is silly to create so many new variables, better to get out a list?)
for (df in all_df) {
paste0("no_reads_", df) <- rowSums(df[,-1]) == 0
paste0(names(all_df), "_neu") <- filter(df, !paste0("no_reads_", df))
}
b) lapply (Here I don't know how to include best the second step of the command)
lapply(seq_along(all_df),
function(df) rowSums(all_df[,-1][[df]]) == 0)
You would help me a lot :)
Best,
Kathrin