I have had this topic already 3 or 4 times and I thought I found a solution, but I did not. I have big problems with converting dataframes like that (one example of many more):
https://megastore.uni-augsburg.de/get/TXLoameX7G/ (I hope its ok to provide the dataframe via my university website)
The original dataframe looks like the one on the left side and I want to have it look like on the right side:
I have this code, that works fine for most of my dataframes (the real dataframes have 31 days, not only 3).
library(tidyverse)
trans_df= df %>% gather(Day, value, Day01:Day31) %>% spread(Station, value)
But for some reason it doesnt work for all of my dataframes. Some are showing this error (like the one I uploded in the link):
Error: Duplicate identifiers for rows (2893, 2905), (19333, 19345), (35773, 35785), (52213, 52225), (68653, 68665), (85093, 85105), (101533, 101545), (117973, 117985), (134413, 134425), (150853, 150865), (167293, 167305), (183733, 183745), (200173, 200185), (216613, 216625), (233053, 233065), (249493, 249505), (265933, 265945), (282373, 282385), (298813, 298825), (315253, 315265), (331693, 331705), (348133, 348145), (364573, 364585), (381013, 381025), (397453, 397465), (413893, 413905), (430333, 430345), (446773, 446785), (463213, 463225), (479653, 479665), (496093, 496105), (2894, 2906), (19334, 19346), (35774, 35786), (52214, 52226), (68654, 68666), (85094, 85106), (101534, 101546), (117974, 117986), (134414, 134426), (150854, 150866), (167294, 167306), (183734, 183746), (200174, 200186), (216614, 216626), (233054, 233066), (249494, 249506), (265934, 265946), (282374, 282386), (298814, 298826), (315254, 315266), (331694, 331706), (348134, 348146), (364574, 364586), (381014, 381026),
I already asked here how to solve this problem: R - Wrong error message - Error: Duplicate identifiers for rows
I got an answer to do this:
data2 <- data %>%
gather(Day, value, Day01:Day31) %>%
tibble::rowid_to_column() %>%
spread(Station, value)
First I thought it is working, because I dont get the Duplicate identifiers Error anymore, but the file sizes are getting huge and it seems to duplicate each line 4 times!
Any idea how to finally solve this problem?