Search for a column index for a specific cost

I have brain cramps. Below is a toy data set:

df <- data.frame( id = 1:6, v1 = c("a", "a", "c", NA, "g", "h"), v2 = c("z", "y", "a", NA, "a", "g"), stringsAsFactors=F) 

I have a specific value that I want to find in a set of specific columns, and I want to determine the position in which it is. The fields I'm looking for are symbols, and the trick is that the value I'm looking for may not exist. In addition, null rows are also present in the dataset.

Assuming I knew how to do this, the position of the variable indicates the values ​​that I would like to return.

 > df id v1 v2 position 1 1 az 1 2 2 ay 1 3 3 ca 2 4 4 <NA> <NA> 99 5 5 ga 2 6 6 hg 99 

The general rule is that I want to find the position of the value "a", and if it is not or not v1, then I want to return 99.

In this case, I look at v1 and v2, but actually I have 10 different variables. It is also worth noting that the value I'm looking for can only exist once in 10 variables.

What is the best way to generate this recode?

Thank you very much in advance.

+4
source share
2 answers

Use match :

 > df$position <- apply(df,1,function(x) match('a',x[-1], nomatch=99 )) > df id v1 v2 position 1 1 az 1 2 2 ay 1 3 3 ca 2 4 4 <NA> <NA> 99 5 5 ga 2 6 6 hg 99 
+9
source

First, leave the first column:

 df <- df[, -1] 

Then do something like this (disclaimer: I feel terribly sleepy * ):

 ( df$result <- unlist(lapply(apply(df, 1, grep, pattern = "a"), function(x) ifelse(length(x) == 0, 99, x))) ) v1 v2 result 1 az 1 2 ay 1 3 ca 2 4 <NA> <NA> 99 5 ga 2 6 hg 99 

* sleepy = code is not portable

EDIT (a slightly different solution, I still feel sleepy):

 df$result <- rapply(apply(df, 1, grep, pattern = "a"), function(x) ifelse(length(x) == 0, 99, x)) 
+1
source

Source: https://habr.com/ru/post/1341379/


All Articles