在R中来回虚拟变量

fio*_*ual 5 r vectorization dummy-data

所以,我已经使用R开关两年了,并且一直试图获得矢量化的全部概念.由于我从调查的多个响应集中处理了很多虚拟变量,因此我认为用这种情况学习会很有趣.

我们的想法是从多个响应变为虚拟变量(并返回),例如:"在这8种不同的巧克力中,哪些是您最喜欢的(最多选择3种)?"

有时候,我们编写这为虚拟变量(1人喜欢你的"蔚蓝或者",0人不喜欢它),每个选项1个变量,有时为分类(1人喜欢你的"蔚蓝或者",2人喜欢你的'瑞士莲’等),3个变量的3种选择.

所以,基本上我最终会得到一个矩阵,其中的线条就像

1,0,0,1,0,0,1,0
Run Code Online (Sandbox Code Playgroud)

或者像行的矩阵

1,4,7
Run Code Online (Sandbox Code Playgroud)

如上所述,这个想法是从一个到另一个.到目前为止,我为每个案例提供了一个循环解决方案,并提供了从虚拟到分类的矢量化解决方案.我将不胜感激任何进一步了解此问题以及分类到虚拟步骤的矢量化解决方案.

愚蠢而不是愚蠢

vecOrig<-matrix(0,nrow=18,ncol=8)  # From this one
vecDest<-matrix(0,nrow=18,ncol=3)  # To this one

# Populating the original matrix.
# I'm pretty sure this could have been added to the definition of the matrix, 
# but I kept getting repeated numbers.
# How would you vectorize this?
for (i in 1:length(vecOrig[,1])){               
vecOrig[i,]<-sample(vec)
}

# Now, how would you vectorize this following step... 
for(i in 1:length(vecOrig[,1])){            
  vecDest[i,]<-grep(1,vecOrig[i,])
}

# Vectorized solution, I had to transpose it for some reason.
vecDest2<-t(apply(vecOrig,1,function(x) grep(1,x)))   
Run Code Online (Sandbox Code Playgroud)

不要愚蠢到虚伪

matOrig<-matrix(0,nrow=18,ncol=3)  # From this one
matDest<-matrix(0,nrow=18,ncol=8)  # To this one.

# We populate the origin matrix. Same thing as the other case. 
for (i in 1:length(matOrig[,1])){         
  matOrig[i,]<-sample(1:8,3,FALSE)
}

# this works, but how to make it vectorized?
for(i in 1:length(matOrig[,1])){          
  for(j in matOrig[i,]){
    matDest[i,j]<-1
  }
}

# Not a clue of how to vectorize this one. 
# The 'model.matrix' solution doesn't look neat.
Run Code Online (Sandbox Code Playgroud)

Sve*_*ein 4

矢量化解决方案:

假人到不假人

vecDest <- t(apply(vecOrig == 1, 1, which))
Run Code Online (Sandbox Code Playgroud)

不是假人到假人(回到原来)

nCol <- 8

vecOrig <- t(apply(vecDest, 1, replace, x = rep(0, nCol), values = 1))
Run Code Online (Sandbox Code Playgroud)