I've the following table
Owner | Pet | Housing_Type |
---|---|---|
A | Cats;Dog;Rabbit | 3 |
B | Dog;Rabbit | 2 |
C | Cats | 2 |
D | Cats;Rabbit | 3 |
E | Cats;Fish | 1 |
The code is as follows:
Data_Pets = structure(list(Owner = structure(1:5, .Label = c("A", "B", "C", "D",
"E"), class = "factor"), Pets = structure(c(2L, 5L, 1L,4L, 3L), .Label = c("Cats ",
"Cats;Dog;Rabbit", "Cats;Fish","Cats;Rabbit", "Dog;Rabbit"), class = "factor"),
House_Type = c(3L,2L, 2L, 3L, 1L)), class = "data.frame", row.names = c(NA, -5L))
Can anyone advise me how I can create new columns based on the data in Pet column by creating a new column for each animal separated by ; to look like the following table?
Owner | Cats | Dog | Rabbit | Fish | Housing_Type |
---|---|---|---|---|---|
A | Y | Y | Y | N | 3 |
B | N | Y | Y | N | 2 |
C | N | Y | N | N | 2 |
D | Y | N | Y | N | 3 |
E | Y | N | N | Y | 1 |
Thanks!