0

I have a list of patients and the hospitals where they are treated. However one of the hospitals is listed as being in another state so I wanna change it

I'm trying to do it like this:

db <- ifelse(db$hospital=="NYC Hospital", db$State=="New York"),)

However it says I'm missing the NO argument. How can I get around this?

r2evans
  • 141,215
  • 6
  • 77
  • 149
mabj4815
  • 5
  • 2
  • A couple of things wrong here. (1) You are replacing the whole `data.frame` object (inferring) with a vector. This is not incorrect syntax, but seems suspect. (2) The error cannot be more clear: the `no=` argument to the `ifelse` function is **required**, and you are not providing it. The `ifelse` function, if it "spoke", says *"when the `test=` is true then return `yes=`, otherwise return `no=`"* (for each condition in the `test=` vector, which should be the same length as `yes=`/`no=`). – r2evans Jul 29 '20 at 20:14
  • 1
    Please provide sample data with `dput(x)` where `x` is a *small* sample of your `db` variable that includes that hospital (positive test) and other hospitals (negative test). Refs: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Jul 29 '20 at 20:15

1 Answers1

0

Just leave it the same when the answer is NO:

db$State <- ifelse(db$hospital == "NYC Hospital", "New York", db$State)

Next time try using the code syntax

Fernando
  • 126
  • 9
  • When I do this, it changes the names of all the states into numbers, and sets New York as state "0" – mabj4815 Jul 29 '20 at 20:12
  • 1
    A great example of why you need to provide sample data. It's likely you have `factor`s in your data. – r2evans Jul 29 '20 at 20:13
  • if you don't mind changing between `factor` and `character` then you can use this at the begining to avoid the problem: `db$State <- as.character(db$State)` and `db$hospital <- as.character(db$hospital)` – Fernando Jul 29 '20 at 20:18
  • Changed it from factor to character and it works now. thanks! – mabj4815 Jul 29 '20 at 20:20