tfr950
tfr950

Reputation: 422

Check if an index exists in an R vector

I have a simple problem... I would like to check if a specific index exists in a vector in R.

Here are a few examples that need to be satisfied.

#check to see index 18 exists in this vector

empt_vect<- vector(mode = "character")
#Since empty_vect[18] does not exist, return F

large_vect<- vector(mode = "character", length = 100)
#Since large_vect[18] exists, return T

small_vect<- (mode = "character", length = 10)
#since small_vect[18] does not exist, return F

Is there an easy way of doing this? I can't seem to find any functions to do so. Any help is greatly appreciated.

Upvotes: 1

Views: 353

Answers (1)

tfr950
tfr950

Reputation: 422

#check to see index 18 exists in this vector

empt_vect<- vector(mode = "character")
#Since empty_vect[18] does not exist, return F
ifelse(length(empt_vect)>= 18, T,F)

large_vect<- vector(mode = "character", length = 100)
#Since large_vect[18] exists, return T
ifelse(length(large_vect)>= 18, T,F)

small_vect<- vector(mode = "character", length = 10)
#since small_vect[18] does not exist, return F
ifelse(length(small_vect)>= 18, T,F)

Upvotes: 2

Related Questions