-1

Hi Everyone the following two lines of code give me the desired outcome

missing=with(pima,glucose==0|diastolic==0|triceps==0|bmi==0)
missing=with(pima,missing<-glucose==0|diastolic==0|triceps==0|bmi==0)

However the third line fails

missing=with(pima,missing=glucose==0|diastolic==0|triceps==0|bmi==0)

Could I get the semantics of each of these lines and an explanation of why the third line fails?

johnson
  • 283
  • 2
  • 9
  • 2
    What do you mean the third line fails? Does it produce an error? If so, what is the error message? – G5W Aug 03 '18 at 01:01
  • 1
    It would help if you give a [mcve]. See [How to make a great R reproducible example](https://stackoverflow.com/q/5963269/4996248) to see what this means for R. – John Coleman Aug 03 '18 at 01:04

1 Answers1

0

Did you take a look at ?with; it's seems pretty well explained there. Let's take mtcars as an example.

Case 1

with(mtcars, cyl == 4 | am == 1)

Returns a logical vector in your current environment.

Case 2

with(mtcars, var <- cyl == 4 | am == 1)

Returns a logical vector and stores the vector in the local environment constructed from mtcars.

From ?with

Note that assignments within ‘expr’ take place in the constructed environment and not in the user's workspace.

Case 3

with(mtcars, var = cyl == 4 | am == 1)

with tries to evaluate var = (cyl == 4 | am == 1) which fails because

eval(mtcars$var = mtcars$cyl == 4 | mtcars$am == 1)

fails.

Maurits Evers
  • 49,617
  • 4
  • 47
  • 68