Right now I am cleaning up some No2 emission data in U.S, and the format of the data is like:
现在我正在美国清理一些No2排放数据,数据格式如下:
lat long no2
xxx xxx xxx
xxx xxx xxx
xxx xxx xxx
But what I want eventually is a data matrix with latitude as row names and longitude as column names, such as
但我最终想要的是一个数据矩阵,其中纬度为行名,经度为列名,例如
long1 long2 long3 ...
lat1 xxxx xxxx xxxx
lat2 xxxx xxxx xxxx
lat3 xxxx xxxx xxxx
...
Also, since I don't necessarily have data for each cell in the matrix above, I want the places with missing data marked "0".
此外,由于我不一定有上面矩阵中每个单元格的数据,我希望缺少数据的地方标记为“0”。
What I previously did was:
我之前做的是:
one <- read.table(files[1], header = TRUE, sep = ",")
one <- one[,1:3]
row_name <- seq(25.05,49.95, by = 0.1)
col_name <- seq(-124.95, -65.05, by = 0.1)
a <- matrix(0, length(row_name), length(col_name))
data_matrix <- data.frame(a)
row.names(data_matrix) <- row_name
names(data_matrix) <- col_name
for (i in 1: dim(one)[1]){
lat <- as.character(one[i,]$lat)
long <- as.character(one[i,]$long)
data_matrix[lat,long] <- one[i,]$no2
}
This method is apparently slow and inefficient, since basically what I did is go through the entire matrix and fill in the blanks cell by cell.
这种方法显然是缓慢而低效的,因为基本上我所做的是遍历整个矩阵并逐个单元填充空白。
I would really appreciate it if someone can help me solve this!
如果有人能帮助我解决这个问题,我将非常感激!
1 个解决方案
#1
1
It's just a guess (although it's areasonably good guess) until you post some data but until that happens consider this:
这只是一个猜测(虽然这是一个非常好的猜测),直到你发布一些数据,但直到发生这种情况,请考虑这个:
grid_mat <- xtabs( no2 ~ lat +lon, data=one)
#1
1
It's just a guess (although it's areasonably good guess) until you post some data but until that happens consider this:
这只是一个猜测(虽然这是一个非常好的猜测),直到你发布一些数据,但直到发生这种情况,请考虑这个:
grid_mat <- xtabs( no2 ~ lat +lon, data=one)