[英]How to calculate a new column using individual values of other columns in a formula?
I have a data frame with one column X that looks like this: 我有一个带有一栏X的数据框,看起来像这样:
X
1 8
2 4
3 2
4 5
5 3
6 2
7 1
8 5
Using the values in this column I want to create a new column Z that uses the following formula to calculate the new values: 使用此列中的值,我想创建一个新的Z列,该列使用以下公式来计算新值:
So for example, to calculate Z1, the calculation would look like this:
因此,例如,要计算Z1,计算将如下所示:
and Z1 would have a value of 0.005.
而Z1的值为0.005。
Similarly, Z2 would have a value of -0.229 and Z3 a value of 0.107. 同样,Z2的值为-0.229,Z3的值为0.107。
I hope this example makes it clear what I want to achieve for my new Z-column. 我希望这个例子可以清楚地说明我要为新的Z柱实现的目标。 Any idea on how to solve this easily with R?
关于如何使用R轻松解决此问题的任何想法? Maybe in a loop?
也许是一个循环?
Thankful for any tips! 感谢任何提示!
Maybe the following calculates what you want - at least it reproduces your first 3 given numbers: 也许以下内容可以计算出您想要的内容-至少它可以重现您的前3个给定的数字:
(y$Z <- sapply(seq_len(nrow(y)), function(k) {
i <- seq_len(nrow(y))
j <- seq_len(k)
sum((y$X[i[-j]-k]-mean(y$X))*(y$X[i[-j]]-mean(y$X))) / sum((y$X-mean(y$X))^2)
}))
#[1] 0.00528169 -0.22887324 0.10739437 0.07746479 -0.29049296 -0.32042254
#[7] 0.14964789 0.00000000
It can be improved not to calculate the same values again and again. 可以改进为不一次又一次地计算相同的值。
Data: 数据:
y <- data.frame(X=c(8,4,2,5,3,2,1,5))
You can use a while loop to achieve this. 您可以使用while循环来实现此目的。
Here's the dummy data: 这是虚拟数据:
dat <- data.frame(x=c(8,4,2,5,3,2,1,5))
Here's the while loop operation: 这是while循环操作:
func1 <- function(x){
len <- length(x)
i <- 1
z <- vector("integer",length=len)
d <- (x - mean(x))
while(i < length(x)){
z[i] <- sum(d[i]*(x[i+1]-mean(x)))/(sum(d^2))
i = i + 1
}
return(z)
}
The output is of course the returned vector of the same length as x
. 输出当然是返回的向量,其长度与
x
相同。 You can then append z
to your original data frame: 然后,您可以将
z
附加到原始数据框:
dat$z <- func1(dat$x)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.