我有一个data.frame,其中存储了以下信息:
X1 - 数据
X2 - 小时(godzina)
对于上面提到的数据,我有一个名为Punkt(1,1),Punkt(1,2)的列,其中第二行可以找到质心值(例如19.65,49.5)和降雨量以下。
Data <- structure(list(X1 = c("data", "20140509", "20140509", "20140509",
"20140509", "20140509", "20140509", "20140509", "20140509", "20140509",
"20140509", "20140509", "20140509", "20140509", "20140509"),
X2 = c("godzina", "0", "1", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "11", "12", "13"), `Punkt(1,1)` = c("19.55,49.5",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0.25", "0",
"0", "0.01", "0"), `Punkt(1,2)` = c("19.55,49.55", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0.4", "0", "0", "0.01",
"0"), `Punkt(1,3)` = c("19.55,49.6", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0.3", "0", "0", "0", "0")), .Names = c("X1",
"X2", "Punkt(1,1)", "Punkt(1,2)", "Punkt(1,3)"), row.names = c(NA,
15L), class = "data.frame")
我的目标是为每个点和日期时创建一个栅格,并且每一个小时创建一个栅格。
到目前为止,我已经成功地做了一点。要获得列中每个栅格单元的范围,我需要进行以下计算:
Coordinates = Data[1,][3]
xmin = as.numeric(substr(Coordinates, 1,5)) - 0.025
xmax = as.numeric(substr(Coordinates, 1,5)) + 0.025
ymin = as.numeric(substr(Coordinates, 7,11)) - 0.025
ymax = as.numeric(substr(Coordinates, 7,11)) + 0.025
我需要定义降雨率(让我们试试积极的一个):
Rainfall = as.numeric(Data[,3][11])
然后我可以创建一个光栅文件:
r1 <- raster(nrows=1, ncols=1, xmn=xmin, xmx=xmax, ymn=ymin, ymx=ymax)
r1 <- setValues(r1, Rainfall)
plot(r1)
有人可以帮助我吗?
答案 0 :(得分:1)
积分(在闲暇时打开包装:)
pts <- matrix(as.numeric(unlist(strsplit(unlist(Data[1,-c(1:2)]), ","))), ncol=2, byrow=TRUE)
结合价值观:
v <- apply(as.matrix(Data[-1, -c(1:2)]), 1, as.numeric)
pv <- cbind(pts, v)
“姓名”(日期/时间)
nms <- apply(Data[-1,1:2], 1, function(i) paste(i, collapse='_'))
要使此玩具示例正常工作,我们需要在栅格中添加多行。下面介绍了这一点,但你不应该用你的真实数据做到这一点:
pv <- rbind(pv, pv)
pv[1:3,1] <- 20
使用x,y和值的矩阵,您可以使用rasterFromXYZ:
library(raster)
r <- rasterFromXYZ(pv)
names(r) <- nms
r
#class : RasterBrick
#dimensions : 3, 2, 6, 14 (nrow, ncol, ncell, nlayers)
#resolution : 0.45, 0.05 (x, y)
#extent : 19.325, 20.225, 49.475, 49.625 (xmin, xmax, ymin, ymax)
#coord. ref. : NA
#data source : in memory
#names : X20140509_0, X20140509_1, X20140509_2, X20140509_3, X20140509_4, X20140509_5, X20140509_6, X20140509_7, X20140509_8, X20140509_9, X20140509_10, X20140509_11, X20140509_12, X20140509_13
#min values : 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.25, 0.00, 0.00, 0.00, 0.00
#max values : 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.40, 0.00, 0.00, 0.01, 0.00