how to create a space-wide data frame in R

504 views Asked by At

I have spatial temporal data: Daily measurements of 36 measurement stations for 425 days. I want to do some analysis with these data in R, so I just created a data frame from the data, which looks like this: space-wide data frame in R

For each of the Stations X10004, X10007, ... I have the latitude/longitude values, but I do not know how to add these information correctly to the data frame in order to use the available analysis tools of R.

How to do it? Or should I use other data structure possibilities of R and how?

1

There are 1 answers

2
BrodieG On BEST ANSWER

You need to melt your data into long format, and then merge. Once you've done that, you can use ddply/data.table/ggplot etc.

library(reshape2)
res <- merge(
  melt(df, id.vars="date"), 
  lat.lon, 
  by.x="variable", by.y="loc.name"
)
head(res)
#   variable       date value       lat      lon 
# 1       V1 2013-01-01     4 0.6193299 0.815607
# 2       V1 2013-01-02     5 0.6193299 0.815607
# 3       V1 2013-01-03     2 0.6193299 0.815607
# 4       V1 2013-01-04     3 0.6193299 0.815607
# 5       V1 2013-01-05    10 0.6193299 0.815607
# 6       V1 2013-01-06     7 0.6193299 0.815607

In this case, think of variable as station in your data. And here is the dummy data I created:

df <- cbind(
  data.frame(seq(as.Date("2013-01-01"), by="+1 day", length.out=10)), 
  as.data.frame(replicate(10, sample(1:10)))
)
names(df) <- c("date", paste0("V", 1:10))
head(df)
#         date V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
# 1 2013-01-01  4  9  5 10  8  5  7  9  1   1
# 2 2013-01-02  5 10  4  6  6  8  4  6  7  10
# 3 2013-01-03  2  8  1  5  5  3 10  4  9   4
# 4 2013-01-04  3  3 10  4  3  7  9  7  5   5
# 5 2013-01-05 10  6  9  7 10 10  5  5  3   6
# 6 2013-01-06  7  2  2  9  4  2  2  8  8   3
lat.lon <- data.frame(loc.name=paste0("V", 1:10), lat=runif(10), lon=runif(10))
head(lat.lon)
#   loc.name       lat       lon
# 1       V1 0.6193299 0.8156070
# 2       V2 0.3656795 0.9293682
# 3       V3 0.7073155 0.1494767
# 4       V4 0.6715280 0.9029310
# 5       V5 0.3588971 0.2281054
# 6       V6 0.7231073 0.2840767