-2

I try to performe the Cowles-Jones-Test in R.

Basically, I don't know how to write a script, in which I can check, if a value within my vector is bigger than zero or not (for every single value in this vector). If it's bigger than zero, it should replace this number with an one, if not, than a zero.

Couldn't find anything so far and would appreciate some help

zx8754
  • 52,746
  • 12
  • 114
  • 209
Chris M
  • 5
  • 2
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jan 16 '20 at 18:00

3 Answers3

0

Assigning values to your vector

Assuming you have a vector named myVector, you can replace its values using this code:

myVector <- c(1, 2, 3, 45, 56, 0,-1, -99, 12, -5)
myVector[myVector > 0] <- 1     #Replace values > 0 with 1
myVector[!myVector > 0] <- 0    #Replace values not > 0 with 0

The result will be:

> myVector
1 1 1 1 1 0 0 0 1 0

Hope this helps.

Louis
  • 3,592
  • 2
  • 10
  • 18
0

You can use ifelse to make both transformations at once:

yourvector<-ifelse(yourvector>0,1,0)

The third argument is the "else".

iod
  • 7,412
  • 2
  • 17
  • 36
0

Conversion to binary can be done with

as.integer(vec > 0)

Or

+(vec > 0)
akrun
  • 874,273
  • 37
  • 540
  • 662