-3

I have a very large data frame with Timestamp, StationId and Value as column names.

I would like to create a new matrix where the rows are Timestamps, columns are StationIds and the matrix elements are Values.

I have tried doing so using a loop but it is taking very long:

for (row in 1:nrow(res)) 
{
rmatrix[toString(res[row,"Timestamp"]),toString(res[row,"StationId"])] <- 
res[row,"Value"]
}

The 'res' data frame looks like this. The timestamps are for a year, at 5mins interval. There are 62 unique station ids. The elements in the Value column are actually rainfall values.

The rmatrix I'm trying to rearrange the data into looks like this. Each row is a unique timestamp at 5mins interval. Each column is the id of a station. The elements of the matrix are supposed to be the rainfall value for that station at that time.

Is there a faster way to do this?

jsong005
  • 17
  • 3

1 Answers1

0
library(tidyverse)

df <- res %>% spread(StationIds,Values)
Mahdi Baghbanzadeh
  • 540
  • 1
  • 4
  • 10