I am trying to use mutate function in dplyr to createa string feature. Basically, if a given value is present in another dataframe, I want to retain the value. Otherwise, I wat to replace it with unknown. When I try the following code, all values always come out as "unknown" regardless of whether they are present in the dataframe I am using to lookup. what am i doing wrong?
Asked
Active
Viewed 69 times
-1
-
It would be easier to help if you create a small reproducible example along with expected output. Read about [how to give a reproducible example](http://stackoverflow.com/questions/5963269). Images are not the right way to share data/code. – Ronak Shah Jul 20 '21 at 03:53
1 Answers
1
dataframe_name['column_name']
returns a dataframe. You need a vector in ifelse
which can be achieved with either [[
or $
so you should use dataframe_name[['column_name']]
or dataframe_name$column_name
.
For example using mtcars
dataset as an example see the difference in the outputs.
mtcars['cyl'] %in% 6
#[1] FALSE
mtcars[['cyl']] %in% 6
# [1] TRUE TRUE FALSE TRUE FALSE TRUE FALSE FALSE FALSE TRUE TRUE FALSE
#[13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#[25] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE
mtcars$cyl %in% 6
# [1] TRUE TRUE FALSE TRUE FALSE TRUE FALSE FALSE FALSE TRUE TRUE FALSE
#[13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#[25] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE
This might be a useful read - The difference between bracket [ ] and double bracket [[ ]] for accessing the elements of a list or dataframe

Ronak Shah
- 377,200
- 20
- 156
- 213