0

This may seem rather trivial, but I barely don't know any R at all and google-fu didn't help.

So, scenario is as follows: I have a dataframe with 37 columns, and I need to filter it, keeping only those rows that contain a certain string in any of 2 columns (EMPRESA, PAIS).

What I've tried so far:

        if(input$ExperienciasSearchHidden == "")
        {
        }
        else
        {
            print(colnames(DFfilters))
            DFfilters <- DFfilters  [which  (
                                                    DFfilters$EMPRESA       ==  "ESPAÑAAA"
                                                &   DFfilters$PAIS          ==  "ESPAÑAAA"
                                            )
                                    ,]
        }

Output (ERROR):

Warning: Error in if: missing value where TRUE/FALSE needed
  [No stack trace available]

Any help would be MUCH appreciated!

1
  • NOTE: I'm using the operator == and a hardcoded value to simplify as much my problem. Commented Mar 12, 2021 at 14:57

2 Answers 2

1

There is nothing wrong with the second part of the code with the subsetting but the problem is in the if statement. It is likely that input$ExperienciasSearchHidden is NA.

DFfilters=data.frame(EMPRESA=c("a", "ESPAÑAAA", "b"), PAIS=c(NA,"ESPAÑAAA", "a"), stringsAsFactors = FALSE)
DFfilters  [which  (
  DFfilters$EMPRESA       ==  "ESPAÑAAA"
  &   DFfilters$PAIS          ==  "ESPAÑAAA"
)
,]
   EMPRESA     PAIS
2 ESPAÑAAA ESPAÑAAA

NA==""
NA

if (NA) {
  print("problem")
}
Error in if (NA) { : missing value where TRUE/FALSE needed
Sign up to request clarification or add additional context in comments.

3 Comments

I'm a complete noob at R, but I don't think that's the problem, cuse I ``` print(paste('ExperienciasSearchHidden=' , toString(input$ExperienciasSearchHidden) , sep=""))``` and it prints a string :S
Is the string that it prints ‘ExperienciasSearchHiddenNA’? How about class(input$ExperienciasSearchHidden) and what does it say. Try toString(input$ExperienciasSearchHidden) inside the if statement, you can only compare strings to “”
It seems that the Warning: Error in if: missing value where TRUE/FALSE needed camed a couple lines below my if-else, on an operation not ready to accept an empty dataframe. I'll gladly accept @Stacker answer and post a new one, since I'm not able to filter the dataframe properly :'(
0

In the absence of reproducible data, here's a solution with mock data:

library(dplyr)
df %>%
  rowwise() %>%
  filter(any(str_detect(c_across(), 'EMPRESA|PAIS')))
# A tibble: 3 x 2
# Rowwise: 
  v1      v1.1   
  <chr>   <chr>  
1 x       PAIS   
2 EMPRESA x      
3 x       EMPRESA

Data:

df <- data.frame(
  v1 = c("x", "EMPRESA", "y", "z", "x"),
  v1 = c("PAIS", "x", "y", "z", "EMPRESA")
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.