对列表中的所有数据框执行相同的操作

发布于 2025-01-11 01:53:43 字数 1326 浏览 1 评论 0原文

我有一个数据框列表,我需要对所有数据框执行相同的操作。我不确定是否最好创建一个函数或循环,然后将其应用于列表中的所有内容。我都尝试过,但我无法让任何东西发挥作用。我已经尝试过:

    prepare_df <- function(df) {
      # make changes to df
      names(df)[1] <- 'row'
      df <- df %>%
        pivot_longer((!row), names_to = "plateColumn", values_to = "Broth_t0")
      df <- df %>%
        mutate(wellID = paste0(row, plateColumn)) %>%
        select(-c(row, plateColumn))
      return(df)

并且我已经尝试过:

      change <- function(i){
        i <- i %>% 
          pivot_longer((!row), names_to = "plateColumn", values_to = "Treatment") %>% 
          mutate(get(wellID = paste0(row, plateColumn))) %>%
          select(-c(row, plateColumn))
      }

我想知道是否更容易一步一步地完成我需要做的事情,而不是尝试将所有内容合并到 1 个函数/循环中,但我需要能够完成所有这些工作以下。 mr1_44_312_t0 是单独的数据帧,我只是复制和粘贴,但我有太多数据,无法继续使用该方法。我对 R 相当陌生,所以任何帮助将不胜感激!

    mr1_44_312_t0 <- mr1_44_312_t0[36:43, 2:14]
    colnames(mr1_44_312_t0) [2:13] <- (1:12)
    names(mr1_44_312_t0)[1] <- 'row'
    mr1_44_312_t0 <- mr1_44_312_t0 %>%
      pivot_longer((!row), names_to = "plateColumn", values_to = "Absorbance_t0")
    mr1_44_312_t0 <- mr1_44_312_t0 %>%
      mutate(wellID = paste0(row, plateColumn)) %>%
      select(-c(row, plateColumn))

I have a list of data frames and I need to do the same thing to all of them. I'm not sure if it would be best to create a function, or a loop and then apply it to everything in the list. I have tried both and I cannot get anything to work. I have tried:

    prepare_df <- function(df) {
      # make changes to df
      names(df)[1] <- 'row'
      df <- df %>%
        pivot_longer((!row), names_to = "plateColumn", values_to = "Broth_t0")
      df <- df %>%
        mutate(wellID = paste0(row, plateColumn)) %>%
        select(-c(row, plateColumn))
      return(df)

and I have tried:

      change <- function(i){
        i <- i %>% 
          pivot_longer((!row), names_to = "plateColumn", values_to = "Treatment") %>% 
          mutate(get(wellID = paste0(row, plateColumn))) %>%
          select(-c(row, plateColumn))
      }

I'm wondering if it is easier to do what I need to do step by step instead of trying to incorporate everything into 1 function/loop but I need to be able to do all of that is below. mr1_44_312_t0 is the individual dataframe and I have been just copying and pasting, but I have too much data to continue with that method. I am fairly new to R, so any help would be appreciated!

    mr1_44_312_t0 <- mr1_44_312_t0[36:43, 2:14]
    colnames(mr1_44_312_t0) [2:13] <- (1:12)
    names(mr1_44_312_t0)[1] <- 'row'
    mr1_44_312_t0 <- mr1_44_312_t0 %>%
      pivot_longer((!row), names_to = "plateColumn", values_to = "Absorbance_t0")
    mr1_44_312_t0 <- mr1_44_312_t0 %>%
      mutate(wellID = paste0(row, plateColumn)) %>%
      select(-c(row, plateColumn))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人生百味 2025-01-18 01:53:43

我们可以使用map来循环list

library(dplyr)
library(tidyr)
library(purrr)
out <- map(lst1, ~ .x %>%
          # select the columns 2 to 14
          select(2:14) %>%
          # subset the rows from 36:43
          slice(36:43) %>% 
          # reshape to long format        
          pivot_longer(-1, names_to = "plateColumn", 
          values_to = "Absorbance_t0") %>%
          # create the wellID
          mutate(wellID = paste0(cur_data()[[1]], plateColumn)) %>%
           select(-1, -plateColumn))

We may use map to loop over the list

library(dplyr)
library(tidyr)
library(purrr)
out <- map(lst1, ~ .x %>%
          # select the columns 2 to 14
          select(2:14) %>%
          # subset the rows from 36:43
          slice(36:43) %>% 
          # reshape to long format        
          pivot_longer(-1, names_to = "plateColumn", 
          values_to = "Absorbance_t0") %>%
          # create the wellID
          mutate(wellID = paste0(cur_data()[[1]], plateColumn)) %>%
           select(-1, -plateColumn))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文