0

I have a dataframe with 4 colums here is the structure.

I would like to create a new vector (valuetofind) with the value of 1 if all my rows have the number 1 and -1 if all my rows have the number -1.
Otherwhise just fill with NA.

str(results)
'data.frame':   435 obs. of  4 variables:
 $ model.knn: Factor w/ 2 levels "-1","1": 2 2 2 2 2 2 2 1 2 2 ...
 $ p.arbre  : Factor w/ 2 levels "-1","1": 2 2 1 1 2 2 2 1 2 2 ...
 $ p.svm    : Factor w/ 2 levels "-1","1": 2 2 2 2 2 2 2 2 2 2 ...
 $ p.rf     : Factor w/ 2 levels "-1","1": 2 2 2 1 2 2 2 1 2 2 ...


model.knn p.arbre p.svm p.rf  Valuetofind
    1        1      1    1         1
   -1       -1      1    1        NA
   -1       -1     -1   -1        -1

I have been trying many things but I am blocked

I tried to convert to numeric, the facor in my dataframe. It gave me values of 2 and 1 instread of my -1 and 1.

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
  • Try `as.numeric(as.character())` – AntoniosK May 24 '18 at 13:41
  • Expanding on @AntoniosK, try `2*as.numeric(as.character()) - 1` to get values `-1` and `1`. – Rui Barradas May 24 '18 at 13:52
  • @RuiBarradas bad advice. You could do `2*as.numeric() - 1` if you want to make strong assumptions about the data (or if you wanted to convert a factor with two arbitrary levels to -1, 1), but since the levels are already `"-1"` and `"1"`, `as.numeric(as.character())` will work perfectly, and the `as.character` in your suggestion is a bug and will give a bad result. – Gregor Thomas May 24 '18 at 13:56
  • @Gregor Right. And I found another bug, the OP wants `-1` or `1` iff all values are `-1` or ´1`, otherwise `NA`. I will answer. – Rui Barradas May 24 '18 at 14:00
  • Thank you, Converting in not really a problem now. I am looking a way to compare the vectors with an efficient way – henry dupuis May 24 '18 at 14:01

1 Answers1

0

Use nested ifelse() and rowSums() for base solution.

results <- data.frame(model.knn = c(1, -1, -1), p.arbre = c(1, -1, -1), p.svm = c(1, 1, -1), p.rf = c(1, 1, -1))

results["Valuetofind"] <- ifelse(rowSums(results) == ncol(results), 1, 
                                  ifelse(rowSums(results) == -ncol(results), -1, NA))

results
  model.knn p.arbre p.svm p.rf Valuetofind
1         1       1     1    1           1
2        -1      -1     1    1          NA
3        -1      -1    -1   -1          -1

You can try dplyr::case_when() if there are multiple ifelse() nesting.

JdeMello
  • 1,708
  • 15
  • 23