Mary A. Marion
Mary A. Marion

Reputation: 800

R print one element of a vector

This is a programming question. I am not understanding how to print out 1 element of a vector. My code is given below:

# creating a range of values to test for accuracy
thresh=seq(0,1,by=0.05)
acc = matrix(0,1,20)
# computing accuracy for different threshold values from 0 to 1 step by 0.05
for (i in 1:21){
  matrix = confusion.matrix(obs,pred,threshold=thresh[i])
  acc[i]=(matrix[1,1]+matrix[2,2])/nrow(test)
  acc[i]}
max(acc)
library(nnet)
which.is.max(acc)
acc
#Q how can I print only thresh[12] the first max(acc)? 
   there may be more than one thresh with the max(acc).
print(thresh)

???

Upvotes: 0

Views: 706

Answers (1)

Lloyd Christmas
Lloyd Christmas

Reputation: 1038

If I understand what you're asking, this should work. Here's a simple example.

a <- c(1,2,3,3)
thresh <- c(1,2,3,4)

# run at command line
> thresh[a==max(a)]
  [1] 3 4

Upvotes: 2

Related Questions