Tuesday, 15 April 2014

Counting the number of times a value change signs with R -


I am very new to R but encouraged with it because I find it accessible though I am not a programmer Trying to get the following issue: How many times I get a price change signal in a countdown and then the result will be sorted by the path (The example for table is below - path is a factor). I can understand how to sort the data after eventually receiving them, but so far there is no signal to calculate for the number of numbers - and the one-icon gets a + one Any remedy?

  & gt; Head (Test) Path Instructions 1 1 -3.84089 2 1 -1.12258 3 1 1.47411 4 2 -1.4732 9 5 2 5.4525 6 2 10.161 

I think what you want

  sum (diff (sign)! = 0)   

Where x is a vector, in your case, dat $ direction which you are trying to count sign changes.



If you want to calculate by path , you can use the by function or Use your data.frame in a worth of data and using by abilities

example:

Value X is your original data.frame

  # I'm adding another row to the data, Just to show that it works # (i.e., two paths in a different number Provides) X & lt; - rbind (X, c (2, -5)) # Datatable Library (Data Deleticity) DT & LT; - Change the number of changes in datatable (x) #, change each path DT [, change: = sum (difference (sign) = 0 (comment about  factors ): < / H3> 

If the direction is code> factor , it must first be converted to numeric You can do this

  as DT [, direction: = numeric (direction)]   

Result:
  DT path direction change 1: 1 -3.84089 1 2: 1 -1.12258 1 3: 1 1.47411 1 4: 2 -1 4732 9 2 5: 2 5.45250 2 6: 2 10.16100 2 7: 2 -5.00000 2    

No comments:

Post a Comment