2

So I would like to read in data and do a summation of one of the columns of 18000 points of data. The thing is the summation requires the variable Tc and then to subtract five iterations before. I don't know how to make it start at its summation 5 data points down so it does not give me an error that there is nothing to subtract in the first 4 data points.

Here is what a small portion of the data looks like:

 head(data)
               Time   Record   Ux   Uy    Uz    Ts       Tc       Tn       To       Tp  Tq
1 2016-09-07 09:00:00.1 38651948 0.46 1.21 -0.26 19.53 19.31726 20.43197 19.39093 19.54993 NAN
2 2016-09-07 09:00:00.2 38651949 0.53 1.24 -0.24 19.48 19.30391 20.43744 19.37996 19.51704 NAN
3 2016-09-07 09:00:00.3 38651950 0.53 1.24 -0.24 19.48 19.31249 20.43269  19.3752 19.44648 NAN
4 2016-09-07 09:00:00.4 38651951 0.53 1.24 -0.24 19.48 19.30391 20.40221 19.33919 19.41596 NAN
5 2016-09-07 09:00:00.5 38651952 0.53 1.24 -0.24 19.48 19.24906 20.36079 19.31178 19.38068 NAN
6 2016-09-07 09:00:00.6 38651953 0.51 1.28 -0.28 19.44 19.20519 20.32008 19.30629 19.42693 NAN

Here is the code:

data <- read.csv(('TOA5_10815.raw_data5411_2016_09_07_0900.dat'),
            header = FALSE,
            dec = ",",
            col.names = c("Time", "Record", "Ux", "Uy", "Uz", "Ts", "Tc", "Tn", "To", "Tp", "Tq"),
            skip = 4)

Tc = data$Tc

sum = 0
m = 18000
j = 5

for (k in 1:(m-j)){
    inner = (Tc[[k]]-Tc[[k-j]])
    sum = sum + inner
}
final = 1/(m-j)*sum
rawr
  • 20,481
  • 4
  • 44
  • 78
Megan
  • 29
  • 3
  • add a condition in the loop `if (k <= j) next` and iterate over 1:m ? also you don't need a loop, I think that `1 / (m - j) * sum(Tc[(j + 1):(m - j)] - Tc[1:(m - j * 2)])` will get it – rawr Oct 06 '16 at 21:47
  • Thanks, Ill give it a try! – Megan Oct 06 '16 at 21:59
  • So: `if (k <= j) next( (1 / (m - j) * sum(Tc[(j + 1):(m - j)] - Tc[1:(m - j * 2)]))` Then I can assign that a new variable to view it or save it to a new array? – Megan Oct 06 '16 at 22:09
  • no use the entire vector in `1 / (m - j) * sum(Tc[(j + 1):(m - j)] - Tc[1:(m - j * 2)])` and skip the loop completely – rawr Oct 06 '16 at 22:27
  • `data <- read.csv(('TOA5_10815.raw_data5411_2016_09_07_0900.dat'), header = FALSE, dec = ",", col.names = c("Time", "Record", "Ux", "Uy", "Uz", "Ts", "Tc", "Tn", "To", "Tp", "Tq"), skip = 4) Tc = data$Tc m = 18000 j = 5 summation <- (1 / (m - j) * sum(Tc[(j + 1):(m - j)] - Tc[1:(m - j * 2)])) summation` okay, so its telling me that Warning message: In Ops.factor(Tc[(j + 1):(m - j)], Tc[1:(m - j * 2)]) : ‘-’ not meaningful for factors – Megan Oct 06 '16 at 22:44
  • `m = 18000; j = 5` `Tc` are all numeric? – rawr Oct 06 '16 at 22:46
  • Yeah Tc is above in the question. It has decimals. But that shouldn't change anything right? – Megan Oct 06 '16 at 22:48
  • it's only numeric if `is.numeric(data$Tc)` is true – rawr Oct 06 '16 at 22:50
  • Yikes it says false – Megan Oct 06 '16 at 22:51
  • http://stackoverflow.com/questions/3418128/how-to-convert-a-factor-to-an-integer-numeric-without-a-loss-of-information since it is a factor – rawr Oct 06 '16 at 22:51
  • Perfect thanks so much! You have been so helpful. – Megan Oct 06 '16 at 22:59
  • Hey sorry to bother you again, but could you help me understand why you multiplied by 2 here: `- Tc[1:(m - j * 2)]) ` – Megan Oct 11 '16 at 17:29
  • because k is 1 to m-j so `inner = (Tc[[k]]-Tc[[k-j]])` becomes `Tc[[m - j]]-Tc[[m - j - j]]` at its max value. basically you have to start the loop j iterations in and then you shift the loop by j (if that makes more sense) – rawr Oct 11 '16 at 17:38

1 Answers1

0

Welcome to stackoverflow!

I would suggest you make a more reproducible example for your next questions here (see here).

To answer your question you can either to this in a for loop as you have been working on currently or in much more efficient way; using one type of apply functions (here: lapply). You can read more about these functions here.

Creating data set:

set.seed(1)
Tc<-rnorm(18000) 

The lapply function. Note that we are starting on 6, since Tc[5] - Tc[c(5-5)] would just return Tc[5].

sum<-unlist(lapply(6:18000,function(x) Tc[x]-Tc[c(x-5)]))

Done!

Verifying the function by typing in console:

> head(sum)
[1] -0.1940146  0.3037857  1.5739533 -1.0194995 -0.6348962  2.3322496
> Tc[6]-Tc[1]
[1] -0.1940146
Community
  • 1
  • 1
nadizan
  • 1,323
  • 10
  • 23
  • Thanks so much for your understanding. The only thing is I don't understand how to read in my data set and do that. I got what you did which I am assuming created a random dataset of 18000 points, but I don't understand how the lapply knows to call my data set since I don't see how it calls the random one that you created. – Megan Oct 06 '16 at 22:21