R pass value of column as value for selecting column from array
I am trying to pass the value of an existing R attribute (column) as the value needed for identifying the particular column in an array I want to select. I previously wrote an For loop with an IF statement but it is running really slow.
Each person will have a group membership (1, 2, or 3). I have the probability of their group membership in data$prob1, data$prob2, and data$prob3.
I want to pass the value of data$Group as follows:
data$ClstrAffinity = data$Prob[ , data$Group]
but it does not work. Any ideas?
The slow running code is below.
Thank you.
data$ProbOne = data$Prob[ , 1]
data$ProbTwo = data$Prob[ ,2]
data$ProbThree = data$Prob[ ,3]
data$GroupMembershipNumeric = as.numeric(data$Group)
data[data$Group == 1]
for (a in c(1:nrow开发者_Python百科(data))) {
groupMembership = data$GroupMembershipNumeric[a]
if (groupMembership == 1) {
data$ClstrAffinity[a] = data$ProbOne[a]
}
if (groupMembership == 2) {
data$ClstrAffinity[a] = data$ProbTwo[a]
}
if (groupMembership == 3) {
data$ClstrAffinity[a] = data$ProbThree[a]
}
print(groupMembership)
groupMembership = NULL
}
Matrix indexing is what you need. I'll generate some sample data
set.seed(5)
Prob <- matrix(sample(0:10, 15, replace=TRUE)/10, ncol=3)
Group <- sample(1:3,5,replace=TRUE)
Then your desired result is just
ProbFinal <- Prob[cbind(1:5,Group)]
You may use which
:
data$ProbFinal = "NA"
data$ProbOne = data$Prob[ ,1]
data$ProbTwo = data$Prob[ ,2]
data$ProbThree = data$Prob[ ,3]
data$ProbFinal[which(data$Group == 1)] = data$ProbOne[which(data$Group == 1)]
data$ProbFinal[which(data$Group == 2)] = data$ProbTwo[which(data$Group == 2)]
data$ProbFinal[which(data$Group == 3)] = data$ProbThree[which(data$Group == 3)]
精彩评论