Codes Working to other's machine not working for mine

Hello, I am getting an error that
"Error in if (tmp %in% "1" | tmp %in% "0") { :
the condition has length > 1" after I run function code below. However this codes doesn't appear if it run from another machine. I had same issues with dplyr, same codes were working in other's laptops but not working for mine as well!

for (ind in 1:col_num){
  tmp<-restock_constraints[,ind]
  if(tmp %in% "1" | tmp %in% "0" ){
    tmp_count_yes<-sum(tmp %in% "1")
    tmp_prop_yes_2<-(tmp_count_yes/nrow(restock_constraints))
    restock_constraint_matrix[1, ind] = tmp_prop_yes_2
  }else{
    restock_constraint_matrix[1, ind] = NA}}

differences of same code and same data giving different results across machines, is normally addressed by using a package manager ( I recommend renv) that allows the package dependencies to be tracked (the versions used recorded) so that the environment can be sufficiently reproduced.

If you have same code but different data, then all bets are off, as you may simply have been lucky to not have data which exposes a problematic edge case in your code.

It seems you might not understand the error you have seen, so here is a simple script you can step through to understand what is happening.


tmp <- "1"

tmp %in% "1" | tmp %in% "0" 

if(tmp %in% "1" | tmp %in% "0" ){
  print("yay")} else {
    print("nay")
  }


tmp <- c("1","2")

tmp %in% "1" | tmp %in% "0" 

if(tmp %in% "1" | tmp %in% "0" ){
  print("yay")} else {
    print("nay")
  }

you will see that the code (the if statements) assume that a single TRUE or FALSE value will be assessed and that will determine which curly brace is executed, but in the lower example, there are two values in temp so potentially more than one TRUE/FALSE, and then what branch to execute is unknown.

Now, either in principle tmp should only ever have one value, in which case you should look at correcting earlier code that let restock_contstraints have multiple rows , or else rethink what you are doing. should you be using vectorised ifelse() perhaps, or looping through the rows in a controlled way etc.

p.s. a final note just to say I don't see that you use any dplyr functions in the code fragment you shared, but thats not to say that you didnt use dplyr to get your data together, only we can't see that as its not been shared.

Hello @nirgrahamuk Thanks so much for a reply.
With dply I had same problem. If it runs from another machine the same codes using same data do works with no errors, but I I run from my machine I end up with errors.
Here is the error and codes below; So I was just wandering may be there is a problem with my R? Because similar issues were not occurring if they are run from another machine.
Errors; Error in mutate():
! Problem while computing ..1 = across(everything(), ~replace_na(.x, "n/a")).
Caused by error in across():
! Problem while computing column price_collapsible.
Caused by error in vec_assign():
! Can't convert replace to match type of data .
Run rlang::last_error() to see where the error occurred.

codes;

hygiene_kit <- df_1 %>%
  select(state, county, location,price_soap_bar, price_towel, price_toothpaste, price_toothbrush, price_haircomb, price_rope, price_blade,price_pin)%>% 
  dplyr:: mutate(across(.cols = starts_with("price"), ~tidyr::replace_na(.x, "n/a")))

This seems unrelated to your original post, so should probably best have been its own separate forum thread.

I believe what you shared is going to be a problem on any system, as it probably is trying to place tidyr na replacement char values of "n\a" into what are probably otherwise numeric columns relating to prices ...

Whats confusing me is the errors you shared don't seem to completely match your code. i.e.
the error references across(everthing(), but your code fragment uses starts_with() selector rather than everything()
additionally 'price_collapsible' is referenced in the error, but the select in the fragment would not carry that through for mutate/across in the following step to be aware of.

Thank you once more @nirgrahamuk for great following up. Apology for the mismatching between error message and codes. I have tried to give correct one. However, What I am experiencing here if I could run these dplyr codes and if function(that I shared in the initial post) from other computers, I could end with no errors from neither of them!

Error in mutate():
! Problem while computing ..1 = across(everything(), ~replace_na(.x, "n/a")).
Caused by error in across():
! Problem while computing column price_collapsible.
Caused by error in vec_assign():
! Can't convert replace to match type of data .
Run rlang::last_error() to see where the error occurred.

emergency_kit <- df_1 %>%
  select(state, county, location,price_collapsible, price_noncollapsible, price_bucket_lid,price_soap_bar,aquatabs_price,pur_sachets_price,filter_cloth_price) %>% 
  mutate(across(everything(), ~replace_na(.x, "n/a")))

Why did you choose to write this code ?
Replacing numeric nas with character codes when dealing with a numeric field is generally a bad idea. Do you have context that would make it a good idea ?

I was trying to create a frequency table !

you can make a frequency table without replacing NA values to text, when the frequency table info is expected to be numeric (at least at point of computation).
so, my advice to you is that you drop the replace_na code, or at least move it to a later presentation preparing part of code rather than doing it prematurely before you have calculated the frequencies.

Thanks so much, I will do that for sure!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.