Convert Numeric Vector to Binary (0/1) Based on Limit

Convert numeric vector to binary (0/1) based on limit

You don't need to make newvector beforehand and using it as an argument to ifelse is causing the error.

Try this...

newvector <- ifelse( p>=0.5 , 1 , 0 )

But even better just do it vectorised...

newvector <- as.integer( p >= 0.5 )

Convert numeric vector to 0 and 1

The logical index (v1 > 0) can be coerced to binary by wrapping with +. The TRUE elements change to 1 and FALSE to 0.

+(v1 > 0)
#[1] 1 1 1 0

Other ways of coercion include

(v1 > 0) + 0L

Or

(v1 > 0) * 1

If we need a function

zerone <- function(a) {
+(a > 0)
}

zerone(v1)
#[1] 1 1 1 0

data

v1 <-  c(3, 8, 10, 0)

How to convert integer number into binary vector?

There's the intToBits function that converts any integer to a vector of 32 raws, so you can do this:

decimals <- c(3,5,11,4)
m <- sapply(decimals,function(x){ as.integer(intToBits(x))})
m

> m
[,1] [,2] [,3] [,4]
[1,] 1 1 1 0
[2,] 1 0 1 0
[3,] 0 1 0 1
[4,] 0 0 1 0
[5,] 0 0 0 0
[6,] 0 0 0 0
[7,] 0 0 0 0
[8,] 0 0 0 0
[9,] 0 0 0 0
[10,] 0 0 0 0
[11,] 0 0 0 0
[12,] 0 0 0 0
[13,] 0 0 0 0
[14,] 0 0 0 0
[15,] 0 0 0 0
[16,] 0 0 0 0
[17,] 0 0 0 0
[18,] 0 0 0 0
[19,] 0 0 0 0
[20,] 0 0 0 0
[21,] 0 0 0 0
[22,] 0 0 0 0
[23,] 0 0 0 0
[24,] 0 0 0 0
[25,] 0 0 0 0
[26,] 0 0 0 0
[27,] 0 0 0 0
[28,] 0 0 0 0
[29,] 0 0 0 0
[30,] 0 0 0 0
[31,] 0 0 0 0
[32,] 0 0 0 0

Create all possible combiations of 0,1, or 2 1s of a binary vector of length n

This algorithm might be be more effective than that based on expand.grid:

n <- 3
z <- rep(0,n)

answer <- t(apply(combn(0:n,2),2,function(k) {z[k]=1;z}))
# [,1] [,2] [,3]
# [1,] 1 0 0
# [2,] 0 1 0
# [3,] 0 0 1
# [4,] 1 1 0
# [5,] 1 0 1
# [6,] 0 1 1

[EDIT] I noticed that my original solution misses a trivial case of all zeros,
which can be easily fixed:

rbind(unname(z),answer)
# [,1] [,2] [,3] [,4]
# [1,] 0 0 0 0
# [2,] 1 0 0 0
# [3,] 0 1 0 0
# [4,] 0 0 1 0
# [5,] 0 0 0 1
# [6,] 1 1 0 0
# [7,] 1 0 1 0
# [8,] 1 0 0 1
# [9,] 0 1 1 0
# [10,] 0 1 0 1
# [11,] 0 0 1 1

Converting all binary (0, 1, NA) variables to factors

We can pass two conditions in mutate_if- check the column is numeric (is.numeric) and all the unique values are %in% 0, 1 or NA - to select the columns and then convert it to factor class

library(dplyr)
df %>%
mutate_if(~ is.numeric(.) && all(unique(.) %in% c(0, 1, NA)), factor)
# A tibble: 10 x 5
# X1 X2 X3 X4 X5
# <dbl> <fct> <chr> <int> <fct>
# 1 -0.546 1 a 18 1
# 2 0.537 1 b 1 1
# 3 0.420 1 c 5 1
# 4 -0.584 1 d 20 0
# 5 0.847 1 e 11 0
# 6 0.266 0 f 14 1
# 7 0.445 1 g 6 1
# 8 -0.466 <NA> h 6 0
# 9 -0.848 <NA> i 14 0
#10 0.00231 1 j 3 1

data

set.seed(24)
df <- tibble(X1 = rnorm(10), X2= sample(c(1, 0, NA), 10, replace = TRUE), X3 = letters[1:10], X4 = sample(20, 10, replace = TRUE), X5 = sample(c(1, 0), 10, replace = TRUE))

Convert binary (0|1) numpy to integer or binary-string?

One way would be using dot-product with 2-powered range array -

b.dot(2**np.arange(b.size)[::-1])

Sample run -

In [95]: b = np.array([1,0,1,0,0,0,0,0,1,0,1])

In [96]: b.dot(2**np.arange(b.size)[::-1])
Out[96]: 1285

Alternatively, we could use bitwise left-shift operator to create the range array and thus get the desired output, like so -

b.dot(1 << np.arange(b.size)[::-1])

If timings are of interest -

In [148]: b = np.random.randint(0,2,(50))

In [149]: %timeit b.dot(2**np.arange(b.size)[::-1])
100000 loops, best of 3: 13.1 µs per loop

In [150]: %timeit b.dot(1 << np.arange(b.size)[::-1])
100000 loops, best of 3: 7.92 µs per loop

Reverse process

To retrieve back the binary array, use np.binary_repr alongwith np.fromstring -

In [96]: b = np.array([1,0,1,0,0,0,0,0,1,0,1])

In [97]: num = b.dot(2**np.arange(b.size)[::-1]) # integer

In [98]: np.fromstring(np.binary_repr(num), dtype='S1').astype(int)
Out[98]: array([1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1])

binary vector of max

Here:

X = np.array([2,5,8,1])
one_hot = tf.one_hot(indices=tf.argmax(X), depth=tf.shape(X)[0])

Cleaner way of constructing binary matrix from vector

set.seed(1)
playv <- sample(0:5,20,replace=TRUE)
playv <- as.character(playv)
results <- model.matrix(~playv-1)

The columns in result you may rename.

I like the solution provided by Ananda Mahto and compared it to model.matrix. Here is a code

library(microbenchmark)

set.seed(1)
v <- sample(1:10,1e6,replace=TRUE)

f1 <- function(vec) {
vec <- as.character(vec)
model.matrix(~vec-1)
}

f2 <- function(vec) {
table(sequence(length(vec)), vec)
}

microbenchmark(f1(v), f2(v), times=10)

model.matrix was a little bit faster then table

Unit: seconds
expr min lq median uq max neval
f1(v) 2.890084 3.147535 3.296186 3.377536 3.667843 10
f2(v) 4.824832 5.625541 5.757534 5.918329 5.966332 10


Related Topics



Leave a reply



Submit