对多个 data.tables 执行多个操作
Performing multiple operations on multiple data.tables
我创建了 30 个 table。他们的名字结构如下:
mdl_(种族)_(工资四分位数).
(种族) 是以下之一:白人、黑人、西班牙裔、亚洲人、其他人或所有。
(工资四分位数) 是以下之一:Q1、Q2、Q3、Q4 和所有 Q。
因为我有 6 个种族类别和 5 个工资四分位数,所以我有 6*5 = 30 个对象!
- 示例:线性模型仅包括工资分布的第一个四分位数中的西班牙裔 => mdl_hispanics_Q1
- 示例:包括所有种族和所有工资四分位数的线性模型 => mdl_all_allQ
所有 table 的格式相同,当然具有不同的值:
Variables Estimate Std. Error t value Pr(>|t|)
1: Intercept 37.231178895 9.486380e-02 392.469814 0.000000e+00
2: forborn -0.612941167 5.174224e-02 -11.846051 2.300944e-32
3: female -3.238655089 4.797890e-02 -67.501655 0.000000e+00
4: numchild 0.583390602 2.239027e-02 26.055543 1.841656e-149
5: numchild_female 0.371351058 9.086739e-02 4.086736 4.376191e-05
6: hs 0.173864095 9.180975e-02 1.893743 5.826025e-02
7: somecol 0.595612050 9.407851e-02 6.331011 2.439689e-10
8: college 1.593917949 9.929766e-02 16.051918 5.923264e-58
9: advanced 0.171443556 1.983952e-03 86.415175 0.000000e+00
10: rw -0.001207904 1.460021e-05 -82.731964 0.000000e+00
11: rw_squared -0.954029880 3.252520e-02 -29.332024 8.456547e-189
我想要做的是获得一个包含 30 个值的数值向量,其中每个值都是变量“forborn”的估计值,前提是其统计显着性 Pr(>|t|) < 0.1,否则为零。我是 R 的初学者,只知道如何 table table。这非常乏味并且占用了太多代码。有没有一种方法可以利用 table 的命名方式相似这一事实,并在一次扫描中循环此操作?
编写一个函数,根据 p 值和 lapply
将列 Estimate
提取到列表中。
library(data.table)
fextrac <- function(x){
y <- x[, Estimate := ifelse(`Pr(>|t|)` < 0.1, Estimate, 0)][["Estimate"]]
y[x$Variables == "forborn"]
}
Estimates_list <- sapply(dt_list, fextrac)
Estimates_list
#[1] -0.6129412 -0.6129412
测试数据
dt1 <- read.table(text = "
Variables Estimate 'Std. Error' 't value' 'Pr(>|t|)'
1: Intercept 37.231178895 9.486380e-02 392.469814 0.000000e+00
2: forborn -0.612941167 5.174224e-02 -11.846051 2.300944e-32
3: female -3.238655089 4.797890e-02 -67.501655 0.000000e+00
4: numchild 0.583390602 2.239027e-02 26.055543 1.841656e-149
5: numchild_female 0.371351058 9.086739e-02 4.086736 4.376191e-05
6: hs 0.173864095 9.180975e-02 1.893743 5.826025e-02
7: somecol 0.595612050 9.407851e-02 6.331011 2.439689e-10
8: college 1.593917949 9.929766e-02 16.051918 5.923264e-58
9: advanced 0.171443556 1.983952e-03 86.415175 0.000000e+00
10: rw -0.001207904 1.460021e-05 -82.731964 0.000000e+00
11: rw_squared -0.954029880 3.252520e-02 -29.332024 8.456547e-189
", header = TRUE, check.names = FALSE)
set.seed(2021)
dt2 <- dt1
dt2$`Pr(>|t|)`[sample(nrow(dt2), nrow(dt2)/3)] <- 0.1
setDT(dt1)
setDT(dt2)
dt_list <- list(dt1, dt2)
您可以尝试 mget
遍历数据帧,然后使用 sapply
从中获取数据。
编辑,更改了数据框名称以符合您的描述。
ls()
#[1] "mdl_hispanics_..." "mdl_blacks_..." etc.
as.vector( sapply( mget(
grep("mdl_.*[whites|blacks|hispanics|asians|others|all]",
ls(), value=T) ), function(x)
ifelse( x[x$Variables == "forborn","Pr(>|t|)"] < 0.1,
x[x$Variables == "forborn","Pr(>|t|)"], 0) ) )
#[1] 2.300944e-32 2.300944e-32 0.000000e+00
这可能被认为是更好的方法,它 returns 如果 p 值<0.1,或 0 [不是 p 值本身]
rbindlist(lapply(ls(pattern="mdl_"),get))[
Variables=="forborn",fifelse(`Pr(>|t|)`<0.1,Estimate,0)
]
注意:如果您需要进一步确定对象
,只需调整 ls()
中的 pattern
参数
我创建了 30 个 table。他们的名字结构如下:
mdl_(种族)_(工资四分位数).
(种族) 是以下之一:白人、黑人、西班牙裔、亚洲人、其他人或所有。
(工资四分位数) 是以下之一:Q1、Q2、Q3、Q4 和所有 Q。
因为我有 6 个种族类别和 5 个工资四分位数,所以我有 6*5 = 30 个对象!
- 示例:线性模型仅包括工资分布的第一个四分位数中的西班牙裔 => mdl_hispanics_Q1
- 示例:包括所有种族和所有工资四分位数的线性模型 => mdl_all_allQ
所有 table 的格式相同,当然具有不同的值:
Variables Estimate Std. Error t value Pr(>|t|)
1: Intercept 37.231178895 9.486380e-02 392.469814 0.000000e+00
2: forborn -0.612941167 5.174224e-02 -11.846051 2.300944e-32
3: female -3.238655089 4.797890e-02 -67.501655 0.000000e+00
4: numchild 0.583390602 2.239027e-02 26.055543 1.841656e-149
5: numchild_female 0.371351058 9.086739e-02 4.086736 4.376191e-05
6: hs 0.173864095 9.180975e-02 1.893743 5.826025e-02
7: somecol 0.595612050 9.407851e-02 6.331011 2.439689e-10
8: college 1.593917949 9.929766e-02 16.051918 5.923264e-58
9: advanced 0.171443556 1.983952e-03 86.415175 0.000000e+00
10: rw -0.001207904 1.460021e-05 -82.731964 0.000000e+00
11: rw_squared -0.954029880 3.252520e-02 -29.332024 8.456547e-189
我想要做的是获得一个包含 30 个值的数值向量,其中每个值都是变量“forborn”的估计值,前提是其统计显着性 Pr(>|t|) < 0.1,否则为零。我是 R 的初学者,只知道如何 table table。这非常乏味并且占用了太多代码。有没有一种方法可以利用 table 的命名方式相似这一事实,并在一次扫描中循环此操作?
编写一个函数,根据 p 值和 lapply
将列 Estimate
提取到列表中。
library(data.table)
fextrac <- function(x){
y <- x[, Estimate := ifelse(`Pr(>|t|)` < 0.1, Estimate, 0)][["Estimate"]]
y[x$Variables == "forborn"]
}
Estimates_list <- sapply(dt_list, fextrac)
Estimates_list
#[1] -0.6129412 -0.6129412
测试数据
dt1 <- read.table(text = "
Variables Estimate 'Std. Error' 't value' 'Pr(>|t|)'
1: Intercept 37.231178895 9.486380e-02 392.469814 0.000000e+00
2: forborn -0.612941167 5.174224e-02 -11.846051 2.300944e-32
3: female -3.238655089 4.797890e-02 -67.501655 0.000000e+00
4: numchild 0.583390602 2.239027e-02 26.055543 1.841656e-149
5: numchild_female 0.371351058 9.086739e-02 4.086736 4.376191e-05
6: hs 0.173864095 9.180975e-02 1.893743 5.826025e-02
7: somecol 0.595612050 9.407851e-02 6.331011 2.439689e-10
8: college 1.593917949 9.929766e-02 16.051918 5.923264e-58
9: advanced 0.171443556 1.983952e-03 86.415175 0.000000e+00
10: rw -0.001207904 1.460021e-05 -82.731964 0.000000e+00
11: rw_squared -0.954029880 3.252520e-02 -29.332024 8.456547e-189
", header = TRUE, check.names = FALSE)
set.seed(2021)
dt2 <- dt1
dt2$`Pr(>|t|)`[sample(nrow(dt2), nrow(dt2)/3)] <- 0.1
setDT(dt1)
setDT(dt2)
dt_list <- list(dt1, dt2)
您可以尝试 mget
遍历数据帧,然后使用 sapply
从中获取数据。
编辑,更改了数据框名称以符合您的描述。
ls()
#[1] "mdl_hispanics_..." "mdl_blacks_..." etc.
as.vector( sapply( mget(
grep("mdl_.*[whites|blacks|hispanics|asians|others|all]",
ls(), value=T) ), function(x)
ifelse( x[x$Variables == "forborn","Pr(>|t|)"] < 0.1,
x[x$Variables == "forborn","Pr(>|t|)"], 0) ) )
#[1] 2.300944e-32 2.300944e-32 0.000000e+00
这可能被认为是更好的方法,它 returns 如果 p 值<0.1,或 0 [不是 p 值本身]
rbindlist(lapply(ls(pattern="mdl_"),get))[
Variables=="forborn",fifelse(`Pr(>|t|)`<0.1,Estimate,0)
]
注意:如果您需要进一步确定对象
,只需调整ls()
中的 pattern
参数