简体   繁体   English

在R的数据帧列表上应用自定义的function

[英]Apply self-defined function on list of data frames in R

This is a follow-up to a previous question of mine: Code in R to conditionally subtract columns in data frames这是我之前的问题的后续: R 中的代码以有条件地减去数据帧中的列

I now want to apply the given solution to my previous problem我现在想将给定的解决方案应用于我以前的问题

cols <- grep('^\\d+$', names(df), value = TRUE)
new_cols <- paste0(cols, '_corrected')
df[new_cols] <- df[cols] - df[paste0('Background_', cols)]
df[c("Wavelength", new_cols)]

to every data frame in a list.到列表中的每个数据框。 I import all sheets of an excel file so that every sheet becomes one data frame in a list using this code (courtesy of Read all worksheets in an Excel workbook into an R list with data.frames 's top answer):我导入 excel 文件的所有工作表,以便使用此代码将每个工作表都变成列表中的一个数据框(由Read all worksheets in an Excel workbook into an ZE1E1D3D40573127E9EE0480CAF1 提供

read_excel_allsheets <- function(filename, tibble = FALSE) {
  sheets <- readxl::excel_sheets(filename)
  x <- lapply(sheets, function(X) readxl::read_excel(filename, sheet = X))
  if(!tibble) x <- lapply(x, as.data.frame)
  names(x) <- sheets
  x
}

mysheets <- read_excel_allsheets(file.choose())

How do I apply the first code box to my data frame list?如何将第一个代码框应用于我的数据框列表?

I want to get from something like this:我想从这样的事情中得到:

df_1 <- structure(list(Wavelength = 300:301, Background_1 = c(5L, 3L), 
                     `1` = c(11L, 12L), Background_2 = c(4L, 5L), `2` = c(12L, 10L)), 
                class = "data.frame", row.names = c(NA, -2L))

df_2 <- structure(list(Wavelength = 300:301, Background_1 = c(6L, 4L),
                     `1` = c(10L, 13L), Background_2 = c(5L, 6L), `2` = c(11L, 11L),
                     Background_3 = c(4L, 6L), `3` = c(13L, 13L)),
                class = "data.frame", row.names = c(NA, -2L))

df_list <- list(df_1, df_2)

To something like this:对于这样的事情:

df_1_corrected <- structure(list(Wavelength = 300:301, `1_corrected` = c(6L, 9L),
                                 `2_corrected` = c(8L, 5L)),
                class = "data.frame", row.names = c(NA, -2L))

df_2_corrected <- structure(list(Wavelength =300:301, `1_corrected` = c(4L, 9L),
                                 `2_corrected` = c(6L, 5L),
                                 `3_corrected` = c(9L, 7L)),
                class = "data.frame", row.names = c(NA, -2L))

df_corrected_list <- list(df_1_corrected, df_2_corrected)

actual data excerpt实际数据摘录

Wavelength Background 1        1 Background 2       2 Background 3         3
       300     273290.0 337670.0     276740.0  397530     288500.0  367480.0
       301     299126.7 375143.3     299273.3  432250     310313.3  394796.7

I have read the lapply function would be used for this but i have never used it before, as I am quite the beginner in R.我已经阅读了lapply function 将用于此,但我以前从未使用过它,因为我是 R 的初学者。 Help is much appreciated!非常感谢您的帮助!

You can put the code in a function and apply it for each dataframe in list using lapply :您可以将代码放入 function 并使用 lapply 将其应用于列表中的每个lapply

subtract_values <- function(df) {
  cols <- grep('^\\d+$', names(df), value = TRUE)
  new_cols <- paste0(cols, '_corrected')
  df[new_cols] <- df[cols] - df[paste0('Background ', cols)]
  df[c("Wavelength", new_cols)]
}

lapply(df_list, subtract_values)

#[[1]]
#  Wavelength 1_corrected 2_corrected
#1        300           6           8
#2        301           9           5

#[[2]]
#  Wavelength 1_corrected 2_corrected 3_corrected
#1        300           4           6           9
#2        301           9           5           7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 什么 arguments 用于自定义函数,用于 R 中的数据帧列表 - What arguments for self-defined functions for use on list of data frames in R R - 应用 diff() function 或等效的自定义 function 在 Z1639B13BE20377B20 中的多个列上 - R - apply diff() function or equivalent self-defined function on multiple columns in a data.table 如何按列对数据进行分组并将自定义函数应用于每个小组 - How to group data by column and apply a self-defined function to each small group 如何对group_by的结果应用自定义函数 - How to apply self-defined function on the result of group_by 将用户定义的函数应用于数据帧列表 - Apply a user defined function to a list of data frames R data.table 使用自定义函数创建汇总统计表的方式 - R data.table way to create summary statistics table with self-defined function 一个简单的可重现示例,在 ZE1E1D3D405731837E 中的自定义 function 中将 arguments 传递给 data.table - A simple reproducible example to pass arguments to data.table in a self-defined function in R 如何在linux shell命令中运行自定义的R函数? - how to run a self-defined R function in linux shell command? R:如何使用外部自定义函数 - R : How to use self-defined function with outer 如何将参数输入传递给R中的自定义函数? - How to pass argument input to a self-defined function in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM