[英]looping dataframes with different number of columns in r
Maybe is something trivial but I am trying to solve this problem: 也许有些琐碎,但我正在尝试解决此问题:
I have to data frames, one with 25 and another with 9 columns. 我必须要数据帧,一个有25列,另一个有9列。 Now, what I need to do is to fit polynomial equations where my dependent variable is in the data frame with 25 columns and my independent variable is in the data frame with 9 columns.
现在,我需要做的是拟合多项式方程,其中我的因变量在25列的数据框中,而我的自变量在9列的数据框中。 At the moment I combined the columns together and created a data frame called "my.data", so I am looping over the dependent variables using one independent variable at the time.
目前,我将各列组合在一起,并创建了一个名为“ my.data”的数据框,因此我当时使用一个自变量来遍历因变量。 But, I would like do the functions in the loop 25 * 9 times automatically.
但是,我想自动执行25 * 9次循环中的功能。 Is there any way to do that?
有什么办法吗?
setwd("C:\\......")
my.data <- read.table("MyData.txt", header = TRUE, sep = "\t")
for(i in seq_along(my.data))
{
fit1b <- lm(my.data[ ,i] ~ my.data$V1)
fit2b <- lm(my.data[ ,i] ~ poly(my.data$V1, 2, raw=TRUE))
fit3b <- lm(my.data[ ,i] ~ poly(my.data$V1, 3, raw=TRUE))
poly1 <-capture.output(summary(fit1b))
poly2 <-capture.output(summary(fit2b))
poly3 <-capture.output(summary(fit3b))
con = file(description = "MyResults.txt", open="a")
write.table(poly1, file= con, append = TRUE, quote=F, col.names=FALSE, row.names= F)
write.table(poly2, file= con, append = TRUE, quote=F, col.names=FALSE, row.names= F)
write.table(poly3, file= con, append = TRUE, quote=F, col.names=FALSE, row.names= F)
close(con)
}
This is a perfect opportunity to use mapply
and expand.grid
这是使用
mapply
和expand.grid
的绝佳机会
For example. 例如。
# some dummy data
xx <- data.frame(replicate(5, runif(50)))
yy <- setNames(data.frame(replicate(3, runif(50))), paste0('Y',1:3))
# all combinations
cs <- expand.grid(list(pred = names(xx), resp = names(yy)), stringsAsFactors= FALSE)
# a function to do the fitting
fitting <- function(pred, resp, dd){
# fit linear model
ff <- reformulate(pred, resp)
lmf <- lm(ff, data =dd)
# create a formula for poly(,2)
ff.poly2 <- update(ff, .~poly(.,2, raw=TRUE))
# and poly(,3)
ff.poly3 <- update(ff, .~poly(.,3, raw=TRUE))
# fit these models
lmp2 <- lm(ff.poly2, data = dd)
lmp3 <- lm(ff.poly3, data = dd)
# return a list with these three models
list(linear = lmf, poly2 = lmp2, poly3 = lmp3)
}
biglist <- mapply('fitting', pred = as.list(cs[['pred']]),
resp = as.list(cs[['resp']]),
MoreArgs = list(dd = cbind(xx,yy)), SIMPLIFY = FALSE)
# give this list meaningful names
names(biglist) <- do.call(paste, c(cs, sep = ':'))
You can then extract things / summarize things using some nested lapply
statements 然后,您可以使用一些嵌套的
lapply
语句提取事物/总结事物
eg summaries of all the linear models 例如所有线性模型的总结
lapply(lapply(biglist, `[[`,'linear'), summary)
of the quadratic models 二次模型
lapply(lapply(biglist, `[[`,'poly2'), summary)
If you want to extract the information from print(summary(lm))
in a single file, something like 如果要从单个文件中的
print(summary(lm))
中提取信息,则类似
capture.output(lapply(biglist, function(x) lapply(x, summary)), file = 'results.txt')
will create a file called results.txt
with all the results printed there. 将创建一个名为
results.txt
的文件,并在其中打印所有结果。
There is one thing I would like to do, to output the summary rather than the list, but I am not sure is possible to then use the writing function you have. 我想做的一件事情是输出摘要而不是列表,但是我不确定是否可以使用您拥有的书写功能。 Is there any way to obtain that?
有什么办法可以做到这一点?
Call: lm(formula = My-Y-Lable ~ My-X-Label) 通话:lm(公式= My-Y-Lable〜My-X-Label)
Residuals: Min 1Q Median 3Q Max -0.35445 -0.17420 -0.10931 0.06975 0.60246 残数:最小值1Q中位数3Q最大值-0.35445 -0.17420 -0.10931 0.06975 0.60246
Coefficients: Estimate Std. 系数:估计标准 Error t value Pr(>|t|)
误差t值Pr(> | t |)
(Intercept) 0.7560212 0.0720984 10.49 1.24e-14 * (拦截)0.7560212 0.0720984 10.49 1.24e-14 *
My-X-Label 0.0072100 0.0006597 10.93 2.68e-15 * My-X-Label 0.0072100 0.0006597 10.93 2.68e-15 *
Signif. 签名 codes: 0 ' ' 0.001 ' ' 0.01 ' ' 0.05 '.'
代码:0 '' 0.001 '' 0.01 '' 0.05 ''。 0.1 ' ' 1
0.1''1
Residual standard error: 0.2812 on 54 degrees of freedom Multiple R-squared: 0.6887, Adjusted R-squared: 0.6829 F-statistic: 119.5 on 1 and 54 DF, p-value: 2.676e-15 残留标准误差:54个自由度上的0.2812多个R平方:0.6887,调整后的R平方:0.6829 F统计量:在1和54 DF上为119.5,p值:2.6676e-15
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.