简体   繁体   English

在 R 的 data.frames 列表中使用一个 data.frame 的指定列

[英]Use a specified column of one data.frame within a list of data.frames in R

I want to subtract the "response" column of a certain data.frame from a list of data.frames in R.我想从 R 中的 data.frames 列表中减去某个 data.frame 的“响应”列。 Below is a subset of the data, and I want to use the "response" of B1.xls as a control, from which all the other "response" columns (A1.xls and A2.xls) are subtracted.下面是数据的一个子集,我想使用 B1.xls 的“响应”作为控件,从中减去所有其他“响应”列(A1.xls 和 A2.xls)。 Any suggestions to achieve it in a fast way?有什么建议可以快速实现吗? Thank you.谢谢你。

$A1.xls
    time   response
1 1320.0 0.00978786
2 1320.2 0.01236774
3 1320.4 0.01582583
4 1320.6 0.01947132
5 1320.8 0.02356580
6 1321.0 0.02786478

$A2.xls
    time   response
1 1320.0 0.00792504
2 1320.2 0.01079251
3 1320.4 0.01420215
4 1320.6 0.01848047
5 1320.8 0.02318325
6 1321.0 0.02836512

$B1.xls
    time   response
1 1320.0 0.00380097
2 1320.2 0.00515638
3 1320.4 0.00658969
4 1320.6 0.00803828
5 1320.8 0.00980306
6 1321.0 0.01187936

An option is lapply .一个选项是lapply Assuming that we want to only update the list elements with names that starts with "A", use lapply to loop over the subset of the list based on the grep on the names , then use transform to update the response by subtracting from the 'response' column from 'B1.xls' and assign it back to the subset list假设我们只想更新名称以“A”开头的list元素,使用lapply根据names上的grep循环遍历list的子集,然后使用transform通过从 'response 中减去来更新response ' 来自 'B1.xls' 的列并将其分配回子集list

nm1 <- grep("^A\\d+\\.xls", names(lst1))
#or
nm1 <- setdiff(names(lst1), "B1.xls")

lst1[nm1] <- lapply(lst1[nm1], transform,
          response = response - lst1$B1.xls$response)

NOTE: Here, we assume that all the list elements have the same number of rows注意:在这里,我们假设所有list元素的行数相同


or we can use tidyverse approach with map或者我们可以使用tidyverse方法与map

library(purrr)
library(dplyr)
library(stringr)
map_if(lst1, .p = str_detect(names(lst1), "^A\\d+\\.xls$"), ~
       .x %>%
             mutate(response = response - lst1$B1.xls$response))

data数据

lst1 <- list(A1.xls = structure(list(time = c(1320, 1320.2, 1320.4, 1320.6, 
1320.8, 1321), response = c(0.00978786, 0.01236774, 0.01582583, 
0.01947132, 0.0235658, 0.02786478)), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6")), A2.xls = structure(list(time = c(1320, 
1320.2, 1320.4, 1320.6, 1320.8, 1321), response = c(0.00792504, 
0.01079251, 0.01420215, 0.01848047, 0.02318325, 0.02836512)), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6")), B1.xls = structure(list(time = c(1320, 
1320.2, 1320.4, 1320.6, 1320.8, 1321), response = c(0.00380097, 
0.00515638, 0.00658969, 0.00803828, 0.00980306, 0.01187936)), 
class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6")))

You can use purrr::update_list :您可以使用purrr::update_list

library(purrr)

lst1 %>%
  update_list(A1.xls = ~ A1.xls - B1.xls,
              A2.xls = ~ A2.xls - B1.xls)

A base R option using simple for loop使用简单for循环的基本 R 选项

for (k in seq_along(lst)) {
  if (names(lst)[k] != "B1.xls") {
    lst[[k]]$response <- lst[[k]]$response - lst[["B1.xls"]]$response
  }
}

Using Map() :使用Map()

control_df_name <- "B1.xls"

c(Map(function(x){x$response <- x$response - lst1[[control_df_name]]$response; x}, 
    lst1[names(lst1) != control_df_name]), lst1[control_df_name])[names(lst1)]

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

相关问题 R 将列添加到 data.frame 中,即在 data.frames 列表中 - R Add column into data.frame, that is in list of data.frames 从R中的3个data.frames创建一个data.frame - Make a data.frame from 3 data.frames in R 将data.frame列表中的列表取消列出为单个data.frame - Unlist a list within a list of data.frames into a single data.frame 将data.frames列表的列表转换为R中的单个data.frame - Converting a list of list of data.frames to a single data.frame in R 将重叠的data.frames列表转换为单个data.frame - Convert list of overlapping data.frames into single data.frame 通过循环将多个data.frame合并为一个data.frame - Merge several data.frames into one data.frame with a loop 如何将data.frame列表追加到data.frame进行合并? - How to append a list of data.frames to a data.frame for merge? 将列表中的data.frames堆叠到单个data.frame中,并保留名称(列表)作为额外的列 - Stacking data.frames in a list into a single data.frame, maintaining names(list) as an extra column 自动添加存在于一个 data.frame 中但在 R 中的其他 data.frames 中缺失的任何变量 - Automatically add any variables that exist in one data.frame but missing in other data.frames in R R Shiny:如何使用上传的 data.frame 扩展包含 data.frames 列表的反应? - R Shiny: How to expand a reactive containing a list of data.frames with an uploaded data.frame?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM