我有2000列以上的数据应该进行虚拟编码。但是,在某些情况下,值可能会大于1。因此,我想立即对所有这些列进行突变,并将大于1的任何值转换为1。这是前几个数据的摘要列。
我尝试使用mutate_if
,但我认为它仍然是我所需的最佳选择,因为我只需要mutate
数字“代码”列。但是,我的语法不正确...
# the data
d <- tibble(
recordID = c("ID1", "ID2", "ID1", "ID4"),
personNumber = c("1", "1", "2", "1"),
code_1 = c(0, 0, 1, 1),
code_2 = c(0, 2, 0, 0), # this 2 should be a 1
code_3 = c(0, 0, 1, 2), # this 2 should be a 1
code_4 = c(0, 1, 0, 2) # this 2 should be a 1
)
# what it looks like
d
# A tibble: 4 x 6
recordID personNumber code_1 code_2 code_3 code_4
<chr> <chr> <dbl> <dbl> <dbl> <dbl>
1 ID1 1 0 0 0 0
2 ID2 1 0 2 0 1 # this 2 should be a 1
3 ID1 2 1 0 1 0
4 ID4 1 1 0 3 2 # this 3 & 2 should be 1
这是我的尝试,输出应为:
# my attempt
d %>%
mutate_if(is.numeric, ifelse(. >= 1, 1, 0)) # doesn't work
# what it should look like
d
# A tibble: 4 x 6
recordID personNumber code_1 code_2 code_3 code_4
<chr> <chr> <dbl> <dbl> <dbl> <dbl>
1 ID1 1 0 0 0 0
2 ID2 1 0 1 0 1 # 2 has been replaced
3 ID1 2 1 0 1 0
4 ID4 1 1 0 1 1 # 3 & 2 have been replaced
答案 0 :(得分:2)
在这种情况下,无需使用ifelse()
。任何比较的结果都是一个TRUE / FALSE逻辑向量,然后可以将其转换为整数向量:
d %>%
mutate_if(is.numeric, ~ +(. >= 1))
recordID personNumber code_1 code_2 code_3 code_4
<chr> <chr> <int> <int> <int> <int>
1 ID1 1 0 0 0 0
2 ID2 1 0 1 0 1
3 ID1 2 1 0 1 0
4 ID4 1 1 0 1 1
或者:
d %>%
mutate_if(is.numeric, ~ (. >= 1) * 1)
答案 1 :(得分:1)
我们可以使用M=4;
%B is nbins x nbins x nbins x nbins with
B=A(:,:,:,:,1)+A(:,:,:,:,2)+A(:,:,:,:,3);
M=3;
%B is nbins x nbins x nbins with
B_temp=A(:,:,:,:,1)+A(:,:,:,:,2)+A(:,:,:,:,3);
B=B_temp(:,:,:,1)+B_temp(:,:,:,2)+B_temp(:,:,:,3);
M=2
%B is nbins x nbins with
B_temp1=A(:,:,:,:,1)+A(:,:,:,:,2)+A(:,:,:,:,3);
B_temp2=B_temp(:,:,:,1)+B_temp(:,:,:,2)+B_temp(:,:,:,3);
B= B_temp2(:,:,1)+B_temp2(:,:,2)+B_temp2(:,:,3);
来强制逻辑转换为二进制
as.integer