Mariela Contreras
Mariela Contreras

Reputation: 31

Why doesn't a variable created with dplyr exist in the data frame?

I am analyzing a Demographic and Health Survey data set. I am in the phase of creating variables. I created a variable using dplyr and mutate, but when I use the function exist to find out whether it is in the data frame, it does not exist. Why is this so?

datamatrix %>% mutate(hw70_1=case_when(hw70< -200 ~ 1, hw70>= -200 ~ 0))

# A tibble: 91,028 x 44
  X1 caseid order    b4 caseid.1 order.1  hw57 caseid.2 order.2  hw70
  <dbl> <chr>  <chr> <dbl> <chr>    <chr>   <dbl> <chr>    <chr>   <dbl>
 1     1 1 11 ~ b4_01    NA 1 11  2  hw57_1     NA 1 11  2  hw70_1     NA
 2     2 1 21 ~ b4_01     1 1 21  1  hw57_1      4 1 21  1  hw70_1   -103
 3     3 1 21 ~ b4_01     2 1 21  9  hw57_1      2 1 21  9  hw70_1     51
 4     4 1 21 ~ b4_01     1 1 21 13  hw57_1     NA 1 21 13  hw70_1     NA
 5     5 1 21 ~ b4_01    NA 1 21 14  hw57_1     NA 1 21 14  hw70_1     NA
 6     6 1 41 ~ b4_01     2 1 41  2  hw57_1     NA 1 41  2  hw70_1     NA
 7     7 1 41 ~ b4_01     1 1 41  6  hw57_1     NA 1 41  6  hw70_1     NA
 8     8 1 41 ~ b4_01    NA 1 41  7  hw57_1     NA 1 41  7  hw70_1     NA
 9     9 1 51 ~ b4_01     1 1 51  2  hw57_1     NA 1 51  2  hw70_1     NA
10    10 1 61 ~ b4_01     2 1 61  1  hw57_1      9 1 61  1  hw70_1   9999
# ... with 91,018 more rows, and 34 more variables: caseid.3 <chr>,
#   order.3 <chr>, hw71 <dbl>, caseid.4 <chr>, order.4 <chr>, hw72 <dbl>,
#   caseid.5 <chr>, order.5 <chr>, hw73 <dbl>, v005 <dbl>, v013 <dbl>,
#   v020 <dbl>, v021 <dbl>, v022 <dbl>, v025 <dbl>, v044 <dbl>, v106 <dbl>,
#   v137 <dbl>, v190 <dbl>, v437 <dbl>, v438 <dbl>, v445 <dbl>,
#   v447a <dbl>, v453 <dbl>, v501 <dbl>, v502 <dbl>, v714 <dbl>,
#   d005 <dbl>, d104 <dbl>, d106 <dbl>, d107 <dbl>, d108 <dbl>, v213 <dbl>,
#   hw70_1 <dbl>

> exists("hw70_1")
[1] FALSE

Upvotes: 0

Views: 166

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226087

You have two problems:

  • exists("hw70_1") tests whether a variable is present in your workspace (not inside some data frame/tibble)
  • you haven't saved the results of your mutate() call.

Try something like:

dm <- datamatrix %>% mutate(hw70_1=...)
"hw70_1" %in% names(dm)
## or
!is.null(dm[["hw70_1"]])
## or
exists("hw70_1", list2env(dm))

If you prefer, rather than creating a new, modified data frame (dm in the example above), you can overwrite/update datamatrix (datamatrix <- datamatrix %>% mutate(...), or use the %<>% operator: datamatrix %<>% mutate(...)

Upvotes: 2

Related Questions