user3321645
user3321645

Reputation: 3

Subsetting Data and performing series of calculations in for loop

This is my data set:

C1     C2     C3      C4     C5    C6   C7  C8
ATOM    1   -4.794  -7.29   6.756   C   12  1
ATOM    1   -4.357  -6.181  6.473   O   16  1
ATOM    2   -5.279  -7.475  5.986   C   12  1
ATOM    2   -7.564  -8.809  6.984   C   12  1
ATOM    2   -5.822  -7.105  7.238   C   12  1
ATOM    1   -7.515  -10.402 -0.621  C   12  2
ATOM    1   -7.26   -11.716 -0.22   O   16  2
ATOM    1   -8.163  -9.682  0.566   C   12  2
ATOM    2   -6.347  -9.475  -1.255  C   12  2
ATOM    1   -7.302  -8.048  7.702   C   12  3
ATOM    1   -7.676  -8.93   6.667   C   12  3
ATOM    2   -6.864  -9.118  5.529   C   12  3

My goal is to subset the data based the content of column C8 and run series of calculations using a loop. I'm currently doing it manually by running:

sub.1 <- subset(data, C8 == 1)
result.1 <- within(data, {
    multiply.z <- C5 * C7 
    multiply.y <- C4 * C7  
    multiply.x <- C3 * C7 
    Center.z <- sum(multiply.z)/sum(C7) 
    Center.y <- sum(multiply.z)/sum(C7) 
    Center.x <- sum(multiply.z)/sum(C7) 
    #rm(multiply.z,multiply.y,multiply.x) 
})

sub.2 <- subset(data, C8 == 2)
result.2 <- same code as above
sub.3 <- subset(data, C8 == 3)
result.3 <- same code as above

I tried to use a for loop to automatically do the operations above but it's not working. This is my code:

for ( i in 1:max(C8)){
sub.i <- subset(data, C8 == i)
result.i <- within(sub.i, {
    multiply.z <- C5 * C7 
    multiply.y <- C4 * C7  
    multiply.x <- C3 * C7 
    Center.z <- sum(multiply.z)/sum(C7) 
    Center.y <- sum(multiply.z)/sum(C7) 
    Center.x <- sum(multiply.z)/sum(C7) 
    #rm(multiply.z,multiply.y,multiply.x) 
})}

I would appreciate any help or suggestions on how to solve this issue. Thank you in advance!

Upvotes: 0

Views: 56

Answers (2)

Jake Burkhead
Jake Burkhead

Reputation: 6535

Here's a data.table solution:

library(data.table)

DT <- data.table(df)

DT[,
   structure(
       lapply(list(C5, C4, C3), function(x) sum(x * C7) / sum(C7) ),
       names = c("z", "y", "x")
       )
   , by = C8]

##    C8         z          y         x
## 1:  1  6.674000  -7.297562 -5.487813
## 2:  2 -0.370000 -10.426231 -7.316538
## 3:  3  6.632667  -8.698667 -7.280667

Upvotes: 1

Rorschach
Rorschach

Reputation: 32426

You will certainly want to take a close look at the library(plyr) which contains all the tools you want for this type of task. It allows you to complete this task using the function ddply,

library(plyr)

mySubs <- ddply(dat, .(C8), .fun = function(x) {
    multiply.z = x$C5 * x$C7
    multiply.y = x$C4 * x$C7
    multiply.x = x$C3 * x$C7
    Center.z = sum(multiply.z)/sum(x$C7)
    Center.y = sum(multiply.z)/sum(x$C7)
    Center.x = sum(multiply.z)/sum(x$C7)
    ##rm(multiply.z,multiply.y,multiply.x)
    data.frame(z = Center.z, y = Center.y, x = Center.x)
})

Returning a data.frame here (ddply is a function that subsets data.frames and returns data.frames, implied by the 'dd' in its name, a convention used by plyr functions).

> mySubs
  C8         z         y         x
1  1  6.674000  6.674000  6.674000
2  2 -0.370000 -0.370000 -0.370000
3  3  6.632667  6.632667  6.632667

etc...

Upvotes: 1

Related Questions