Tidyr Spread Situation

Hello! I'm an absolute beginner so I basically don't know anything. For my research I need to widen my dataset.

Right now I have all my data in DfAllDataLong
Which is set up like:

Farm | Lifeno | Lactation | WeekInMilk | AvgRuminatingtime | Cow (should be after lifeno but allright..) | AvgEatingTime | AvgInActivityTime | AvgLyingTime | Calcium | Ketosis | Uterus |

The WeekInMilk runs from week -6 op to 0 for everycow, so that is 7 rows per cow indeed. Thats because in the Avg...Time columns it gives the average time that the cow has spent eating per day in that week.

Now i need to get the dataset like:

Farm | Lifeno | Lactation | AvgEatingTimeWeek-6 | AvgEatingTimeWeek-5 | AvgEatingTimeWeek-....--> | AvgEatingTimeWeek-1 | AvgRuminatingTimeWeek-6 | AvgRuminatingTimeWeek-....--> | AvgRuminatingTimeWeek-1 | AvgLegActivityTimeWeek-6 | --> | AvgLegactivityTimeWeek-1 | (Etc for all the AVG Times | Calcium | Ketosis | Uterus|

So spread out up to 35 columns..

I have tried the tydir::spread, and if i try with only 1 of the AVG times mentioned above it 'kinda' works.

DfAllDataWide <- tidyr::spread(DfAllDataLong,
                               key = "WeekInMilk",
                               value = "AvgRuminatingTime")

BUT i seem to only be able to do this with one of the behaviour times. When i try to do this again it says that the key "WeekInMilk" is deleted / not found :stuck_out_tongue: So i now have no idea how to continue.

There is probably a very simple solution but i cannot find it.. could you help me with this?

Thanks in advance!

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

Hey! Unfortunately, there are some confidential issues, as one of the identicators (LifeNo) is a registrated number that can trace back to actual farms :zipper_mouth_face: So im struggling in the way that I cannot really share the dataset.... So im not really sure i can make this reprex?

You omitted it from your screenshot, you can similarly omit it from your replex.
Use dplyr::select function

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.