I am trying to set up an if else statement that allows me to create a third column based on information from two other columns. Here is a sample dataset.
df <- data.frame(RequestTimeSecs1 = c(0, 44, 65, "N/A", 100), RequestTimeSecs2 = c(0,46, 55, 77, 112))
I want to create a third column called "RequestTimeSecs" which takes values from ReqeustTimeSecs1 unless RequestTimeSecs1 has N/A values. In that case, I want to select from RequestTimeSecs2 column.
Here is what I have tried so far.
df$RequestTimeSecs <- ifelse((df$RequestTimeSecs1 != "N/A"), df$RequestTimeSecs1, df$RequestTimeSecs2)
When I do this, I am getting the following results
| RequestTimeSecs1 | RequestTimeSec2 | RequestTimeSecs |
|---|---|---|
| 0 | 0 | 1 |
| 44 | 46 | 3 |
| 65 | 55 | 4 |
| N/A | 77 | 77 |
| 100 | 112 | 2 |
So in RequestTimeSecs, I got the desired input for N/A value, it should be 77 however, I don't know where the rest of the row numbers are coming for. In RequestTimeSecs, I want the following numbers c(0,44,65,77,100).
Please let me know where I am messing up in setting up the if else statement
Aucun commentaire:
Enregistrer un commentaire