Soph2010
Soph2010

Reputation: 613

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
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: enter image description here So for example, to calculate Z1, the calculation would look like this: enter image description here and Z1 would have a value of 0.005.

Similarly, Z2 would have a value of -0.229 and Z3 a value of 0.107.

I hope this example makes it clear what I want to achieve for my new Z-column. Any idea on how to solve this easily with R? Maybe in a loop?

Thankful for any tips!

Upvotes: 4

Views: 97

Answers (2)

GKi
GKi

Reputation: 39717

Maybe the following calculates what you want - at least it reproduces your first 3 given numbers:

(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))

Upvotes: 1

onlyphantom
onlyphantom

Reputation: 9613

You can use a while loop to achieve this.

Here's the dummy data:

dat <- data.frame(x=c(8,4,2,5,3,2,1,5))

Here's the while loop operation:

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. You can then append z to your original data frame:

dat$z <- func1(dat$x)

Upvotes: 0

Related Questions