[英]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))
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.