简体   繁体   English

如何使用公式中其他列的各个值来计算新列?

[英]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.

相关问题 根据 R 中其他列中的值和条件计算新列 - Calculate a new column from values and conditions in the other columns in R 如何使用R中的查找方法基于其他列计算新列? - How to calculate a new column based on other columns using a lookup approach in R? 如何基于其他列按列获取新列中的值 - how to get values in new column based on other column group by columns 如何将特定值保留在列中并将其他值删除到新列 - How to keep specific values in a column and a remove the other values to new columns 如何计算一个公式,该公式采用名称中具有相同后缀的 dataframe 的不同列并创建一个新列? - How to calculate a formula that takes different columns of a dataframe with the same suffix in the name and create a new column? 如何使用新列中的公式计算值用于 R 中新列中的其他行? - How can I use calculated values by formula in a new column for other rows in new column in R? 使用结合其他列条目的公式在数据框中添加新列 - Adding a new column in a data frame using a formula that combines entries from other columns 如何根据其他列将行值连接到新列? - How to concatenate row values to a new column based on other columns? 如何使用R根据其他列的值创建新列 - How to create new columns based on other columns' values using R 如何使用R中的固定公式计算列 - How to calculate a column using a fixed formula in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM