问题
I have a vector of factors given by a sequence of numbers. These factors are also found in separate data seta, called test_set
and train_set
. What the following code does is find where the factor in the data sets matches in the vector of factors and puts a 1 in the place of the matrix. Multiplying this matrix compound_test
by test_set$Compound
should give you compare_comp
.
test_set <- data.frame(Compound=letters[sample(1:3,10,replace = TRUE)])
train_set <- data.frame(Compound=letters[sample(1:3,10,replace = TRUE)])
compare_comp <- letters[1:3]
compound_test <- matrix(0,nrow(test_set),length(compare_comp)) # test indicator matrix
compound_train <-matrix(0,nrow(train_set),length(compare_comp))
for (i in 1:length(compare_comp)){
compound_test[which(compare_comp[i]==test_set$Compound),i]=1
compound_train[which(compare_comp[i]==train_set$Compound),i]=1}
Is there a function in R that lets me create the same thing without the need for a for loop? I have tried model.matrix(~Compound,data=test_set)
but this does not include a column due to the reference level and also produces unwanted column names
回答1:
Easier option is model.matrix
from base R
model.matrix(~ Compound-1, train_set)
model.matrix(~ Compound-1, test_set)
Or table
can also be used if we cbind
with a sequence of rows
table(cbind(nr = seq_len(nrow(train_set)), train_set))
来源:https://stackoverflow.com/questions/59413766/how-can-i-code-this-indicator-matrix-without-using-a-loop-in-r