Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Lappy在R中多次运行来自不同数据帧的变量的模型_R_Loops_Lapply - Fatal编程技术网

如何使用Lappy在R中多次运行来自不同数据帧的变量的模型

如何使用Lappy在R中多次运行来自不同数据帧的变量的模型,r,loops,lapply,R,Loops,Lapply,我有两个数据帧 #虚拟df示例: 种子(1) df1 | t |) #(截距)-0.013981 0.169805-0.0820.936 #predmodex[2]1.000143 0.002357 424.351 | t |) #(截距)-0.013981 0.169805-0.0820.936 #predmodex[2]1.000143 0.002357 424.351 | t |) #(截距)-0.013981 0.169805-0.0820.936 #predmodex[,2]1.000

我有两个数据帧

#虚拟df示例:
种子(1)
df1 | t |)
#(截距)-0.013981 0.169805-0.0820.936
#predmodex[2]1.000143 0.002357 424.351 | t |)
#(截距)-0.013981 0.169805-0.0820.936
#predmodex[2]1.000143 0.002357 424.351 | t |)
#(截距)-0.013981 0.169805-0.0820.936

#predmodex[,2]1.000143 0.002357 424.351由于
df1
df2
具有相同的名称,因此可以执行以下操作:

model <- function(var){
  lm(df1[[var]] ~ df2[[var]])
}
result <- lapply(names(df1)[-1], model)
result

#[[1]]

#Call:
#lm(formula = df1[[var]] ~ df2[[var]])

#Coefficients:
#(Intercept)   df2[[var]]  
#    15.1504      -0.4763  


#[[2]]

#Call:
#lm(formula = df1[[var]] ~ df2[[var]])

#Coefficients:
#(Intercept)   df2[[var]]  
#     3.0227       0.6374  


#[[3]]

#Call:
#lm(formula = df1[[var]] ~ df2[[var]])

#Coefficients:
#(Intercept)   df2[[var]]  
#    15.4240       0.2411  

这回答了你的问题吗@jared_mamrot我想我可以根据自己的需要修改一些帖子。但是我认为,这篇文章的目的和我的不同,所以我会说它没有回答我的问题。我还想用
lappy
来做这件事。从@Ronak Shah的回答中可以看出,我想要的是相当简单的。谢谢你的建议。太棒了,谢谢你澄清:)哦,太简单了!,我离这里很近,再过几天我就到了,哈哈。非常感谢,这正是我想要做的。对不起,我不够冷静,不能给你投票。
purrr::map_df(result, broom::tidy, .id = 'model_num')

#  model_num term        estimate std.error statistic  p.value
#  <chr>     <chr>          <dbl>     <dbl>     <dbl>    <dbl>
#1 1         (Intercept)   15.2       3.03      5.00  0.000194
#2 1         df2[[var]]    -0.476     0.248    -1.92  0.0754  
#3 2         (Intercept)    3.02      4.09      0.739 0.472   
#4 2         df2[[var]]     0.637     0.227     2.81  0.0139  
#5 3         (Intercept)   15.4       4.40      3.50  0.00351 
#6 3         df2[[var]]     0.241     0.272     0.888 0.390