简体   繁体   中英

returning the index of vector for a match

I have a dataframe with first column as the name of the variables i want to match with. This column has 797 observations and is a character type. I have another vector which is again character type and has 31 rows. i want to return the indices of the column of data frame (the one with 787 observations) which matches with the small character vector.

I tried some code using a for loop. but not able to get what i wanted

vec_list<-vector()

for (i in inter_2004_05[,1]){
  for(j in 1:nrow(test_t)){
   if(inter_2004_05[i,1]=test_t[j]){
    vec_list[i]= i
   }
  }
 }

now `` is the vector of words that I want to match. What I expect is that we should check for each element in the first column of the data frame ( inter_2004_05[,1] ) has any match in the character vector ( test_t ). I want the index of inter_2004_05[, 1] where it found a match in test_t

This can be done by using %in% and which.

inter_2004_05  = c('A','B','C','D')
test_t = c('C','B')
indices = which(inter_2004_05 %in% test_t)

As RAB already indicated, match will return the index, so you can right away use those indices to subset what you need. I've just tested the following example:

#Create 2 vectors
word_vector <- c(paste("test", 1:50, sep=""))
test_vector <- c(paste("test", 10:15, sep=""))

#Find the index of every match
matches <- match(word_vector, test_vector)

#Now matches can be used to subset
word_vector2 <- word_vector[matches > 0]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM