Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201945

R and dplyr: how to use ifelse condition with an external dataframe

$
0
0

I'm using dplyr and spark to create a new variable with the mutate command. This new variable new_variable is categorical and must be ALFA if the value of the variable my_data_variable is inside a column of another dataframe other_df$one_column. Consequently its value will be BETA if its value it it is not included in the values ​​of other_df$one_column

an example of what I did:

my_data %>%
     mutate(new_variable = ifelse(my_data_variable == other_df$one_column, "ALFA","BETA"))

but unfortunately I get this error. Even using !!other_df$one_column or local(other_df[['one_column']]) instead of other_df$one_column does not work.

Error: Cannot embed a data frame in a SQL query.

If you are seeing this error in code that used to work, the most likely cause is a change dbplyr 1.4.0. Previously `df$x` or
`df[[y]]` implied that `df` was a local variable, but now you must make that explict with `!!` or `local()`, e.g., `!!df$x` or
`local(df[["y"]))

Are there alternative methods to the ifelse function to get the expected result?


Viewing all articles
Browse latest Browse all 201945

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>