-1

I have temperature dataframe from 37 stations. First column is date and from second to last column are temperature from 1-37 stations. I need to arrange the data in such a way that, the result contains Date column and temperature column from station 1 - 37. Such that the date column is repeated after every station.

I tried

library(dplyr)
tmax_1 %>% select('Date', 'V1')
tmax_2 %>% select('Date', 'V2')
tmax_3 %>% select('Date', V3)
tmax_4 %>% select('Date', V4)
tmax_5 %>% select('Date', V5)
tmax_6 %>% select('Date', V6)

But this takes lot of typing. Can anyone suggest better solution in case i need to do it for more than 1000 stations!!![enter image description here][1]

pogibas
  • 27,303
  • 19
  • 84
  • 117

1 Answers1

0

It looks like you want to make "long" data from "wide" data. tidyr has gather() which does that.

As you have given the header format in your question which you could've done in a better way you can try:

tidyr::gather(df, key = "Station", value = "Temperature", -Date)

In this you'll create a column called Station that will contain the names or the id's of the station acording to the old column names, and the Temperature column that will contain the temperature values acording to the cels. The last argument -Date is to exclude the Date column from undergoing the same transformation. You can learn more about gather() and spread() on the Rstudio blog, and other questions on stackoverflow.

And next time it could be helpful if you gave some visualisation of your data or a reproducible example and the expected result.