My data frame comprises: 1st column is date ( consist of normal and leap year) , 2nd to 32 column is day ( day 1 , day 2... day 31) How to arrange this dataset to a single column.
-
5Welcome to StackOverflow! Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). This will make it much easier for others to help you. – Sotos Jun 18 '19 at 08:18
-
Sounds like a reshaping issue. Have you tried [here](https://stackoverflow.com/questions/12466493/reshaping-multiple-sets-of-measurement-columns-wide-format-into-single-columns) or [here](https://stackoverflow.com/questions/25925556/gather-multiple-sets-of-columns)? – gersht Jun 18 '19 at 09:38
1 Answers
I'm very new to R (and also to SO, at least with answering) but I would use gather
from the tidyr
package for this task.
It's a bit hard to imagine your data without an example, so I'll make my own (Actually I don't see why there should be a date in the first column and then other columns for every single day):
Date day_1 day_2 day_3 day_4 day_5 day_6 ... day_31 ---- ----- ----- ----- ----- ----- ----- ... ------ 1 2019-05-01 1 1 1 0 0 5 ... 6 2 2019-05-02 4 0 2 1 4 5 ... 4 3 2019-05-03 3 2 5 0 5 2 ... 2
You can use gather
to convert your columns to rows the following.
gather(Data, key = "Day", value = "Rainfall", 2:32)
The first parameter (Data
) is your dataset. The key
parameter defines, what name your 'Key' column should have and value
defines the name of the value column. The 2:32
part means, that you want columns 2 to 32 included in the result. If you have no other columns than the ones you mentioned you could also use -1
instead of 2:32
, which just means to ignore the first colum.
This would give you the following result:
Date Day Rainfall 1 2019-05-01 day_1 1 2 2019-05-02 day_1 4 3 2019-05-03 day_1 3 4 2019-05-01 day_2 1 5 2019-05-02 day_2 0 6 2019-05-03 day_2 2 7 2019-05-01 day_3 1 8 2019-05-02 day_3 2 9 2019-05-03 day_3 5 10 2019-05-01 day_4 0 11 2019-05-02 day_4 1 ... ... ...
![](../../users/profiles/3073719.webp)
- 53
- 6