如何找到 Table 行中每个值的 Z 分数?
How to find Z score of each value in row of Table?
我在 R 中有一个 table,如何使行中大于或等于某个数字的值为 1,其余值为 0。例如,如果我的特殊数字是 4,那么我的 table 中每个大于等于 4 的值都为 1,其余为零。例如,这个 table:
a b c d e
Bill 1 2 3 4 5
Susan 4 1 5 4 2
Malcolm 4 5 6 2 1
Reese 0 0 2 3 8
Would Turn Into
a b c d e
Bill 0 0 0 1 1
Susan 1 0 1 1 0
Malcolm 1 1 1 0 0
Reese 0 0 0 0 1
我们可以创建一个TRUE/FALSE
的逻辑矩阵,然后使用+
转换为二进制格式
+(df1>=4)
# a b c d e
#Bill 0 0 0 1 1
#Susan 1 0 1 1 0
#Malcolm 1 1 1 0 0
#Reese 0 0 0 0 1
明确一点,当我们执行 >=
时,它会创建一个 TRUE/FALSE
的逻辑矩阵
df1 >=4
# a b c d e
#Bill FALSE FALSE FALSE TRUE TRUE
#Susan TRUE FALSE TRUE TRUE FALSE
#Malcolm TRUE TRUE TRUE FALSE FALSE
#Reese FALSE FALSE FALSE FALSE TRUE
但是,OP 希望将其转换为 1/0
。通过将 TRUE/FALSE 强制转换为二进制形式,有很多方法可以做到这一点。一种选择是
(df1>=4) + 0L
或
(df1>=4)*1L
或者简单地放置一个 +
将进行强制转换
+(df1>=4)
根据?TRUE
Logical vectors are coerced to integer vectors in contexts where a
numerical value is required, with ‘TRUE’ being mapped to ‘1L’,
‘FALSE’ to ‘0L’ and ‘NA’ to ‘NA_integer_’.
我们也可以用 as.integer
换行,但输出将是一个向量
as.integer(df1>=4)
#[1] 0 1 1 0 0 0 1 0 0 1 1 0 1 1 0 0 1 0 0 1
如果我们将输出分配回原始数据集,我们可以更改该数据集并保持其结构
df1[] <- as.integer(df1>=4)
df1
# a b c d e
#Bill 0 0 0 1 1
#Susan 1 0 1 1 0
#Malcolm 1 1 1 0 0
#Reese 0 0 0 0 1
我在 R 中有一个 table,如何使行中大于或等于某个数字的值为 1,其余值为 0。例如,如果我的特殊数字是 4,那么我的 table 中每个大于等于 4 的值都为 1,其余为零。例如,这个 table:
a b c d e
Bill 1 2 3 4 5
Susan 4 1 5 4 2
Malcolm 4 5 6 2 1
Reese 0 0 2 3 8
Would Turn Into
a b c d e
Bill 0 0 0 1 1
Susan 1 0 1 1 0
Malcolm 1 1 1 0 0
Reese 0 0 0 0 1
我们可以创建一个TRUE/FALSE
的逻辑矩阵,然后使用+
+(df1>=4)
# a b c d e
#Bill 0 0 0 1 1
#Susan 1 0 1 1 0
#Malcolm 1 1 1 0 0
#Reese 0 0 0 0 1
明确一点,当我们执行 >=
时,它会创建一个 TRUE/FALSE
df1 >=4
# a b c d e
#Bill FALSE FALSE FALSE TRUE TRUE
#Susan TRUE FALSE TRUE TRUE FALSE
#Malcolm TRUE TRUE TRUE FALSE FALSE
#Reese FALSE FALSE FALSE FALSE TRUE
但是,OP 希望将其转换为 1/0
。通过将 TRUE/FALSE 强制转换为二进制形式,有很多方法可以做到这一点。一种选择是
(df1>=4) + 0L
或
(df1>=4)*1L
或者简单地放置一个 +
将进行强制转换
+(df1>=4)
根据?TRUE
Logical vectors are coerced to integer vectors in contexts where a numerical value is required, with ‘TRUE’ being mapped to ‘1L’, ‘FALSE’ to ‘0L’ and ‘NA’ to ‘NA_integer_’.
我们也可以用 as.integer
换行,但输出将是一个向量
as.integer(df1>=4)
#[1] 0 1 1 0 0 0 1 0 0 1 1 0 1 1 0 0 1 0 0 1
如果我们将输出分配回原始数据集,我们可以更改该数据集并保持其结构
df1[] <- as.integer(df1>=4)
df1
# a b c d e
#Bill 0 0 0 1 1
#Susan 1 0 1 1 0
#Malcolm 1 1 1 0 0
#Reese 0 0 0 0 1