'Create a new column in R if a previous column had a particular word [duplicate]
I have started learning R and the tidyverse. I want to tidy the Days column in the below table. I want to make it so that there is a new column Control which would be a logical column which would say if the sample was a control sample or a real sample.
I thought about creating a column that takes the value 1 if the Days column had the word control in it and 0 if there was no "control" in it. How can I go about achieving that? Thank you all help is appreciated.
| Name of the sample | Days | Compressive strength (N/mm2) |
|---|---|---|
| Bacillus sphaericus | 7 | 28 |
| Bacillus sphaericus | 14 | 30 |
| Bacillus sphaericus | 28 | 37.3 |
| Bacillus sphaericus | Control 7 | 22 |
| Bacillus sphaericus | Control 14 | 27.65 |
| Bacillus sphaericus | Control 28 | 29.3 |
Solution 1:[1]
After tinkering a little this seems to work.
table$Control <- ifelse(table$Days == "Control 7" | table$Days == "Control 14" | table$Days == "Control 28", table$Control <- 1, table$Control <- 0)
Though this works I don't know if this is the best way to do this, so I would still like to know how other might approach the same problem. Thank you
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | kuruthi |
