I appreciate your time in reading this. I am trying to create a new, wide dataset in r from a long dataset. My dataset is set up something like this:
dd <- read.table(text="Year Basket Fruit
2014 small pear
2014 medium pear
2014 medium orange
2014 large pear
2014 large orange
2014 large apple
2015 small orange
2015 medium pear
2015 medium orange
2015 large pear
2015 large orange
2015 large pomegranate", header=TRUE)
I need the new dataset to have one row per basket type (small, medium, and large), and then a column for each fruit type and year combination, with a yes/no indication of whether that fruit type was present in that basket type that year. Something like this:
out <- read.table(text="
apple.2014 orange.2014 pear.2014 pomegranate.2014 apple.2015 orange.2015 pear.2015 pomegranate.2015
large 1 1 1 0 0 1 1 1
medium 0 1 1 0 0 1 1 0
small 0 0 1 0 0 1 0 0
", header=TRUE)
Any suggestions on how to accomplish this would be extremely appreciated! I have found solutions for how to count the number of unique fruits by basket type, but no solution to create the type of data frame I need. Thanks so much!