df <- data.frame(animal = c("dog", "dog", "cat", "dog", "cat", "cat"),
hunger = c(0, 1, 1, 0, 1,1))
I have a dataframe like the one above with two columns, one containing categories and the other containing binary data.
I am looking to reshape the dataframe to split the category ("animal") column up into two columns of its own with the values of "animal" column as column names and the values of the other column (hunger) as cell values, i.e.
Desired output:
df <- data.frame(dog = c(0, 1, 0),
cat = c(1, 1, 1))
How can I achieve this?
df
was 7 rows anddog
andcat
weren't equal length? (Also, as an aside, I don't think this is a very good plan because the data structure is not very robust.)