Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

r - Looping over a list and get new variables

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Using a for loop:

library(dplyr)

for(i in seq_along(df_list)){
  df_list[[i]] <- df_list[[i]] %>%
    rowwise() %>%
    mutate(sum = sum(c_across(-"OTU_ID")) %>%
    filter(sum > 0)
}

Using purrr::map()

df_list %>%
  map(~ rowwise(.x) %>%
        mutate(sum = sum(c_across(-"OTU ID"))) %>%
        filter(sum > 0))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...