如何使用 for 循环获取我的函数 运行 通过所有值
How to get my function with a for loop to run through all the values
我的函数有问题 'subtract_within'。该函数的目标是获取变量中的一个值,然后将该值与变量中所有其他值的差相加。
当我调用它时,它returns for 循环中第一个元素(正确!)的值,但不是其余元素的值。
这是我的代码:
x<- c(1,2,3,4,5,6) #the value i want to calculate with
y<- c(1,2,3,4,5,6) #the identifier of the value
this_df<- data.frame(x,y)
this_df$z = 0 #now theres a new variable that is 0 for now but will be filled in later
this_df %>%
mutate(z = subtract_within(x,y)) #this is how i will ideally call on this function 'subtract_within'
#start of my function 'subtract_within'
subtract_within<- function(x,y){ #this function takes in two variables from a dataframe
dataframe<- data.frame(x,y) # i locally make it a dataframe
dataframe$z<-0 # i add the variable that i will be returning
for (i in 1:nrow(dataframe)){
dataframe$z[i] <- (dataframe$x[i]*(length(which(dataframe$y != dataframe$y[i])))) - sum(dataframe$x[which(dataframe$y != dataframe$y[i])])
return(dataframe$z)
}
return(dataframe$z)
}
'''
My output is as follows:
x y z
1 1 1 -15
2 2 2 0
3 3 3 0
4 4 4 0
5 5 5 0
ideally my output would be:
x y z
1 1 1 -15
2 2 2 -5
3 3 3 0
4 4 4 5
5 5 5 10
是在for
循环中添加return
的情况。如果我们只使用最后一个return
,它应该可以工作
subtract_within<- function(x,y){ #this function takes in two variables from a dataframe
dataframe<- data.frame(x,y) # i locally make it a dataframe
dataframe$z<-0 # i add the variable that i will be returning
for (i in 1:nrow(dataframe)){
dataframe$z[i] <- (dataframe$x[i]*(length(which(dataframe$y != dataframe$y[i])))) - sum(dataframe$x[which(dataframe$y != dataframe$y[i])])
#return(dataframe$z)
}
return(dataframe$z)
}
我认为问题在于您的循环中有一个 return 语句
for (i in 1:nrow(dataframe)){
dataframe$z[i] <- (dataframe$x[i]*(length(which(dataframe$y != dataframe$y[i])))) - sum(dataframe$x[which(dataframe$y != dataframe$y[i])])
return(dataframe$z) # THIS RIGHT HERE
}
return(dataframe$z)
这使得函数立即return循环后的数据帧只运行一次。如果你删除它,我认为它应该有效。
for (i in 1:nrow(dataframe)){
dataframe$z[i] <- (dataframe$x[i]*(length(which(dataframe$y != dataframe$y[i])))) - sum(dataframe$x[which(dataframe$y != dataframe$y[i])])
}
return(dataframe$z)
我的函数有问题 'subtract_within'。该函数的目标是获取变量中的一个值,然后将该值与变量中所有其他值的差相加。
当我调用它时,它returns for 循环中第一个元素(正确!)的值,但不是其余元素的值。
这是我的代码:
x<- c(1,2,3,4,5,6) #the value i want to calculate with
y<- c(1,2,3,4,5,6) #the identifier of the value
this_df<- data.frame(x,y)
this_df$z = 0 #now theres a new variable that is 0 for now but will be filled in later
this_df %>%
mutate(z = subtract_within(x,y)) #this is how i will ideally call on this function 'subtract_within'
#start of my function 'subtract_within'
subtract_within<- function(x,y){ #this function takes in two variables from a dataframe
dataframe<- data.frame(x,y) # i locally make it a dataframe
dataframe$z<-0 # i add the variable that i will be returning
for (i in 1:nrow(dataframe)){
dataframe$z[i] <- (dataframe$x[i]*(length(which(dataframe$y != dataframe$y[i])))) - sum(dataframe$x[which(dataframe$y != dataframe$y[i])])
return(dataframe$z)
}
return(dataframe$z)
}
'''
My output is as follows:
x y z
1 1 1 -15
2 2 2 0
3 3 3 0
4 4 4 0
5 5 5 0
ideally my output would be:
x y z
1 1 1 -15
2 2 2 -5
3 3 3 0
4 4 4 5
5 5 5 10
是在for
循环中添加return
的情况。如果我们只使用最后一个return
,它应该可以工作
subtract_within<- function(x,y){ #this function takes in two variables from a dataframe
dataframe<- data.frame(x,y) # i locally make it a dataframe
dataframe$z<-0 # i add the variable that i will be returning
for (i in 1:nrow(dataframe)){
dataframe$z[i] <- (dataframe$x[i]*(length(which(dataframe$y != dataframe$y[i])))) - sum(dataframe$x[which(dataframe$y != dataframe$y[i])])
#return(dataframe$z)
}
return(dataframe$z)
}
我认为问题在于您的循环中有一个 return 语句
for (i in 1:nrow(dataframe)){
dataframe$z[i] <- (dataframe$x[i]*(length(which(dataframe$y != dataframe$y[i])))) - sum(dataframe$x[which(dataframe$y != dataframe$y[i])])
return(dataframe$z) # THIS RIGHT HERE
}
return(dataframe$z)
这使得函数立即return循环后的数据帧只运行一次。如果你删除它,我认为它应该有效。
for (i in 1:nrow(dataframe)){
dataframe$z[i] <- (dataframe$x[i]*(length(which(dataframe$y != dataframe$y[i])))) - sum(dataframe$x[which(dataframe$y != dataframe$y[i])])
}
return(dataframe$z)