R 如何将一段代码(而不仅仅是单个函数)应用于数据集中的所有列
我想将这段代码应用于数据集中的每一列。我可以单独运行所有列,但为75个不同的列重复编写代码,并更改代码中的所有名称以匹配每个列名称,这是非常繁琐的。有没有一种方法可以一次单独运行所有列,而无需为每个列单独编写代码R 如何将一段代码(而不仅仅是单个函数)应用于数据集中的所有列,r,R,我想将这段代码应用于数据集中的每一列。我可以单独运行所有列,但为75个不同的列重复编写代码,并更改代码中的所有名称以匹配每个列名称,这是非常繁琐的。有没有一种方法可以一次单独运行所有列,而无需为每个列单独编写代码 max.Width =lmer(mergeCowpeaTEST$max.Width ~ (1|Genotype) + (1|Year) + (1|Genotype:Year) + (1|Rep:Year), data=mergeCowpeaTEST,na.action = na.omi
max.Width =lmer(mergeCowpeaTEST$max.Width ~ (1|Genotype) + (1|Year) + (1|Genotype:Year) + (1|Rep:Year), data=mergeCowpeaTEST,na.action = na.omit)
model.a_max.Width <-lmer(max.Width~ (1|Genotype) + (1|Year) + (1|Genotype:Year) + (1|Rep:Year), data=mergeCowpeaTEST)
alt.est.a_max.Width <- influence(model.a_max.Width, obs=TRUE)
cooks<-cooks.distance(alt.est.a_max.Width)
plot(alt.est.a_max.Width, which="cook", sort=FALSE,main="cook's distance plot of max.Width")
which(residuals(max.Width)>0.10)
which(residuals(max.Width)<(-0.10))
boxplot(residuals(max.Width))
myboxplot<-boxplot(residuals(max.Width))
myboxplot$out
hist(residuals(max.Width))
qqnorm(residuals(max.Width))
pdf("Widiv_max.Width_residual_graphs.pdf",height=8,width=10)
plot(fitted(max.Width),residuals(max.Width), xlab="Predicted values", ylab="Residuals", main="Residual Plot of widiv max.Width")
abline(h=0, col="red")
hist(resid(max.Width),main="histogram of max.Width residuals")
qqnorm(residuals(max.Width), main="Residuals Q-Q Plot");qqline(resid(max.Width))
qqnorm(ranef(max.Width)$Genotype$"(Intercept)", main="Genotypes Q-Q Plot"); qqline(ranef(max.Width)$Genotype$"(Intercept)")
qqnorm(ranef(max.Width)$"Genotype:Year"$"(Intercept)", main="Genotype by Year Q-Q Plot"); qqline(ranef(max.Width)$"Genotype:Year"$"(Intercept)")
plot(alt.est.a_max.Width, which="cook", sort=FALSE,main="cook's distance plot of max.Width")
dev.off()
max.Width=lmer(mergeCowpeaTEST$max.Width~(1|generic)+(1|Year)+(1|generic:Year)+(1|Rep:Year),data=mergeCowpeaTEST,na.action=na.omit)
model.a_max.Width关键在于您将其描述为“仅”一个函数。一个函数可以运行任意数量的东西。你可以让它打印一些东西,然后做一些事情,然后输出一些东西。或者做很多事情。或者玩全球地热核战争。所有这些都在一个单一的功能中
apply( ChickWeight, 2, function(clmn) {
cat("Hi")
cat("Low")
cat("The only way to win is not to play at all")
} )
将您的代码包装在函数中,然后使用lappy
或其相关项。我不确定如何从我的代码创建函数。。。一旦我将代码放入函数中,lapply就有了意义。有关如何执行此操作/在中查找“匿名函数”的任何提示。