Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting the number of times a value change signs with R

Tags:

r

I am very new with R but encouraged with it because I find it accessible although I am not a programmer. I am trying to get around the following issue: I need to count how many times a value change signs in a column and then sort the results by Path (The example for a table is below- Path is a factor). I can figure how to sort the data once I eventually get them but have yet to figure out the count for the number of times a + sign becomes - and a - sign becomes a + one. Any suggestion?

Test <- structure(list(Path = c(1L, 1L, 1L, 2L, 2L, 2L), Direction = c(-3.84089, 
-1.12258, 1.47411, -1.47329, 5.4525, 10.161)), .Names = c("Path", 
"Direction"), class = "data.frame", row.names = c(NA, -6L))
head(Test)
#>   Path    Direction
#> 1    1     -3.84089
#> 2    1     -1.12258
#> 3    1      1.47411
#> 4    2     -1.47329
#> 5    2      5.4525
#> 6    2     10.161
like image 522
Gale2 Avatar asked Jun 20 '13 18:06

Gale2


People also ask

How do I count the number of repeated values in R?

Use the length() function to count the number of elements returned by the which() function, as which function returns the elements that are repeated more than once. The length() function in R Language is used to get or set the length of a vector (list) or other objects.

How do I count the number of times a character appears in a column in R?

To count occurrences between columns, simply use both names, and it provides the frequency between the values of each column. This process produces a dataset of all those comparisons that can be used for further processing. It expands the variety a comparison you can make.

Is there a counting function in R?

COUNTIF Function in R, As we know if we want to count the length of the vector we can make use of the length function. In case you want to count only the number of rows or columns that meet some criteria, Yes we can do it easily.


1 Answers

I think what you are looking for is

 sum(diff(sign(X)) != 0)

where X is the vector, in your case, dat$Direction which you're trying to count sign changes.



If you want to calculate by Path, you can use the by function, or convert your data.frame to a data.table and use the built in by capabilities.

Example:

assuming X is your original data.frame

# I'm adding another row to the data, just to show that it works 
#    (ie, giving the two Path values a different number of rows)
X <- rbind(X, c(2, -5))

# convert to a data.table
library(data.table)
DT <- data.table(X)

# count the number of changes, per path
DT[, changes := sum(diff(sign(Direction)) != 0), by=Path]

Edit (re comment about factors):

if Direction is a factor, it will need to be converted to numeric first. You can do this using

DT[, Direction := as.numeric(Direction)]

Results:

DT

       Path Direction changes
  1:    1  -3.84089       1
  2:    1  -1.12258       1
  3:    1   1.47411       1
  4:    2  -1.47329       2
  5:    2   5.45250       2
  6:    2  10.16100       2
  7:    2  -5.00000       2
like image 133
Ricardo Saporta Avatar answered Sep 29 '22 11:09

Ricardo Saporta