将数值转换为二进制(0/1) [英] 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
如何将其更改为二进制矩阵,即,如果一个人至少有一个水果,无论他有多少水果,那么我记录1,如果没有,则记录0.如下所示
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
推荐答案
这是您的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"))
还有您的矩阵:
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:
我的直觉是简单地认为R中的TRUE
和FALSE
下方是数字1
和0
.如果尝试(不是很好的方法)检查等效性,例如1 == TRUE
或0 == FALSE
,则会得到TRUE
.我的捷径方式(事实证明,比起 correct 花费的时间要长更多,或者至少是在概念上更正确的方式)是仅添加TRUE
和FALSE
,因为我知道R会将逻辑向量强制转换为数字.
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 TRUE
s and FALSE
s, since I know that R would coerce the logical vectors to numeric.
正确或至少更合适的方法是使用as.numeric
转换输出(我认为这就是@ JoshO'Brien打算编写的内容).但是....不幸的是,这删除了输入的维属性,因此您需要将结果向量重新转换为矩阵,事实证明,该矩阵为 静止 的速度要比我在答案中添加0
的速度快.
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.
阅读了评论和批评之后,我想我会再添加一个选项-使用apply
遍历各列并使用as.numeric
方法.与手动重新创建矩阵相比,它的速度要慢,但是比在逻辑比较中添加0
的速度更快..
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
谢谢大家的讨论!
这篇关于将数值转换为二进制(0/1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!