Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert numeric values into binary (0/1)

I have a data frame with counts of different kinds of fruits of different people. Like below

    apple  banana  orange
Tim     3       0       2
Tom     0       1       1
Bob     1       2       2

How can I change it into a binary matrix, i.e. if a person has at least one fruit, no matter how many he has, then the I record 1, if not, record 0. Like below

    apple  banana  orange
Tim     1       0       1
Tom     0       1       1
Bob     1       1       1
like image 447
lolibility Avatar asked Jan 25 '13 16:01

lolibility


People also ask

Are 0 and 1 binary numbers?

A binary number is a number expressed in the base-2 numeral system or binary numeral system, a method of mathematical expression which uses only two symbols: typically "0" (zero) and "1" (one).

What do 0 and 1 mean in binary numbers?

In computer science and mathematics, binary is a system where numbers and values are expressed 0 or 1. Binary is base-2, meaning that it only uses two digits or bits. For computers, 1 is true or "on", and 0 is false or "off". The concept of binary and bits are based on of Boolean Algebra.


2 Answers

Here's your data.frame:

x <- structure(list(apple = c(3L, 0L, 1L), banana = 0:2, orange = c(2L, 
1L, 2L)), .Names = c("apple", "banana", "orange"), class = "data.frame", row.names = c("Tim", 
"Tom", "Bob"))

And your matrix:

as.matrix((x > 0) + 0)
    apple banana orange
Tim     1      0      1
Tom     0      1      1
Bob     1      1      1

Update

I had no idea that a quick pre-bedtime posting would generate any discussion, but the discussions themselves are quite interesting, so I wanted to summarize here:

My instinct was to simply take the fact that underneath a TRUE and FALSE in R, are the numbers 1 and 0. If you try (a not so good way) to check for equivalence, such as 1 == TRUE or 0 == FALSE, you'll get TRUE. My shortcut way (which turns out to take more time than the correct, or at least more conceptually correct way) was to just add 0 to my TRUEs and FALSEs, since I know that R would coerce the logical vectors to numeric.

The correct, or at least, more appropriate way, would be to convert the output using as.numeric (I think that's what @JoshO'Brien intended to write). BUT.... unfortunately, that removes the dimensional attributes of the input, so you need to re-convert the resulting vector to a matrix, which, as it turns out, is still faster than adding 0 as I did in my answer.

Having read the comments and criticisms, I thought I would add one more option---using apply to loop through the columns and use the as.numeric approach. That is slower than manually re-creating the matrix, but slightly faster than adding 0 to the logical comparison.

x <- data.frame(replicate(1e4,sample(0:1e3)))
library(rbenchmark)
benchmark(X1 = {
            x1 <- as.matrix((x > 0) + 0)
          },
          X2 = {
            x2 <- apply(x, 2, function(y) as.numeric(y > 0))
          },
          X3 = {
            x3 <- as.numeric(as.matrix(x) > 0)
            x3 <- matrix(x3, nrow = 1001)
          },
          X4 = {
            x4 <- ifelse(x > 0, 1, 0)
          },
          columns = c("test", "replications", "elapsed", 
                      "relative", "user.self"))
#   test replications elapsed relative user.self
# 1   X1          100 116.618    1.985   110.711
# 2   X2          100 105.026    1.788    94.070
# 3   X3          100  58.750    1.000    46.007
# 4   X4          100 382.410    6.509   311.567

all.equal(x1, x2, check.attributes=FALSE)
# [1] TRUE
all.equal(x1, x3, check.attributes=FALSE)
# [1] TRUE
all.equal(x1, x4, check.attributes=FALSE)
# [1] TRUE

Thanks for the discussion y'all!

like image 127
A5C1D2H2I1M1N2O1R2T1 Avatar answered Oct 03 '22 12:10

A5C1D2H2I1M1N2O1R2T1


I usually use this approach:

df[df > 0] = 1
like image 45
Jeongmin Lee Avatar answered Oct 03 '22 12:10

Jeongmin Lee