我有一个csv文件,其中包含两列:A1和A2。假设我有以下功能:
WKH <- function(A, B){
S <- ((B-A)-(B+A)/2
return(S)
}
那么我怎样才能遍历A列和B列,以便可以估算出两列下所有值的相应WKH。
答案 0 :(得分:0)
我有点不好意思发布这个作为答案,如果你提供了一些示例数据和一些实际代码会更好,请参阅@ jsilber的评论。但我想这就是你想要的:
# create a sample dataframe as it might look like after read.csv()
a <- data.frame( A1 = seq( 10, 19 ), A2 = seq( 20, 19 ) )
# perform the operation that you describe, get a vector as result
WKH <- with( a, ( A2 - A1 ) - ( A2 + A1 ) / 2 )
# no loop necessary, you have all you need at hand:
WKH
[1] -5 -7 -8 -10 -11 -13 -14 -16 -17 -19