繁体   English   中英

r中特殊类型的虚拟代码

[英]Special type of dummy code in r

我想在R中做一个特殊类型的虚拟编码程序。基本上,我想要一个查看序数变量的每个级别并按顺序编码的函数。 这就是我需要的:

Variable_1 --> Variable_1a  Variable_1b  Variable_1c
1               1            0            0
1               1            0            0
2               1            1            0
2               1            1            0
3               1            1            1
3               1            1            1

我可以使用ifelse语句手动执行此操作,但必须有一种更简单的方法。

这里有一种方式:

x <- c(1,1,2,2,2,3,3)
cbind(x,sapply(unique(x),function(y)ifelse(x>=y,1,0)))

# [1,] 1 1 0 0
# [2,] 1 1 0 0
# [3,] 2 1 1 0
# [4,] 2 1 1 0
# [5,] 2 1 1 0
# [6,] 3 1 1 1
# [7,] 3 1 1 1

其他方式:

#simulate your variable
set.seed(1)
var1 <- sample(1:3,10,replace=T)
#initialise the matrix and set the colnames
res <- matrix(0,nrow=length(var1),ncol=max(var1))
colnames(res) <- paste0("Variable_1",letters[1:max(var1)])
#set the 1 elements
res[cbind(rep(1:length(var1),var1),unlist(lapply(var1,seq_len)))] <- 1
x <- c(1,1,2,2,2,3,3)
sapply(1:max(x),`<=`,x)*1
#      [,1] [,2] [,3]
# [1,]    1    0    0
# [2,]    1    0    0
# [3,]    1    1    0
# [4,]    1    1    0
# [5,]    1    1    0
# [6,]    1    1    1
# [7,]    1    1    1

这是使用model.matrix的另一个答案(假设你的变量是x ):

x <- c(1,1,2,2,3,3)
m <- model.matrix(~ 0 + factor(x))
for(i in 1:nrow(m)) m[i, 1:max.col(m, ties.method = "last")[i]] <- 1
m
#    factor(x)1 factor(x)2 factor(x)3
# 1           1          0          0
# 2           1          0          0
# 3           1          1          0
# 4           1          1          0
# 5           1          1          1
# 6           1          1          1
# attr(,"assign")
# [1] 0 1 1
# attr(,"contrasts")
# attr(,"contrasts")$`factor(x)`
# [1] "contr.treatment"

反过来可能有点复杂:

m <- model.matrix(~ 0 + factor(x))
m <- m[,ncol(m):1]
for(i in 1:nrow(m)) m[i, 1:max.col(m, ties.method = "last")[i]] <- 1
m[,ncol(m):1]
#   factor(x)1 factor(x)2 factor(x)3
# 1          1          1          1
# 2          1          1          1
# 3          0          1          1
# 4          0          1          1
# 5          0          0          1
# 6          0          0          1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM