R中的and函数的循环

R中的and函数的循环,r,R,我想做以下工作: #choosing one column called "z" from select select<-read.table(file='dane.txt', header=TRUE) n=select$z #defining f function f<-function(redshift) { 0.2*(sqrt(0.3*((1+redshift)^3)+0.7*((1+redshift)^2)+1))^(-1) } # for all values

我想做以下工作:

#choosing one column called "z" from select 
select<-read.table(file='dane.txt', header=TRUE)
n=select$z

#defining f function
f<-function(redshift) {
  0.2*(sqrt(0.3*((1+redshift)^3)+0.7*((1+redshift)^2)+1))^(-1)
}

 # for all values from column "z" I want to calculate the integral from 0 up to value from column "z"
for(i in n){
  int[i]<-integrate(f(i),0,i)
}
对于z列中的所有值,我想计算从0到z列中的值之间的积分,并且我想将文件中每一行的结果保存在int名称下。
为什么它不起作用?现在它给了我一个错误:error in match.funf:'fi'不是函数、字符或符号。请提供帮助。

因为没有示例数据,所以我假定您的函数是针对这些示例数据定义的:

n = as.vector(runif(n=100, min=-3, max=10))
num.obs = length(n)
按照您所做的那样定义您的功能:

f<-function(redshift) {
  0.2*(sqrt(0.3*((1+redshift)^3)+0.7*((1+redshift)^2)+1))^(-1)
}

f是一个函数。你认为f[i]在做什么?您正在尝试调用该函数吗?这将由fi完成。但看起来您只想将整个函数传递给integrate:integratef,0,i。还有,int来自哪里?请尝试提供并清楚地指出所需的输出。是的,我想调用该函数。在第一次尝试中,我得到了int[I]
int=matrix(NA, nrow=num.obs, ncol=1)
for(i in 1:num.obs){
  int[i]=integrate(f,0,n[i])$value
}