4

我有两个包含纬度、经度和温度数据的数据集。一个数据集对应于一个感兴趣的地理区域,对应的纬度/经度对构成该区域的边界和内容(矩阵维度 = 4518x2)

另一个数据集包含包含感兴趣区域的较大区域的纬度/经度和温度数据(矩阵维度 = 10875x3)。

我的问题是:如何从与第一个数据集的纬度/经度数据匹配的第二个数据集中提取适当的行数据(纬度、经度、温度)?

我尝试了各种“for loops”、“subset”和“unique”命令,但无法获得匹配的温度数据。

提前致谢!


10/31 编辑:我忘了提到我正在使用“R”来处理这些数据。

感兴趣区域的纬度/经度数据以 4,518 个文件的列表形式提供,每个文件的名称中包含纬度/经度坐标:

x<- dir()

lenx<- length(x)

g <- strsplit(x, "_")

coord1 <- matrix(NA,nrow=lenx, ncol=1)  
coord2 <- matrix(NA,nrow=lenx, ncol=1)

for(i in 1:lenx) {  
coord1[i,1] <- unlist(g)[2+3*(i-1)]  
coord2[i,1] <- unlist(g)[3+3*(i-1)]     
} 

coord1<-as.numeric(coord1)  
coord2<-as.numeric(coord2)

coord<- cbind(coord1, coord2)

纬度/经度和温度数据是从 NCDF 文件中获得的,其中包含 10,875 个纬度/经度对的温度数据:

long<- tempcd$var[["Temp"]]$size[1]   
lat<- tempcd$var[["Temp"]]$size[2]   
time<- tempcd$var[["Temp"]]$size[3]  
proj<- tempcd$var[["Temp"]]$size[4]  

temp<- matrix(NA, nrow=lat*long, ncol = time)  
lat_c<- matrix(NA, nrow=lat*long, ncol=1)  
long_c<- matrix(NA, nrow=lat*long, ncol =1)  

counter<- 1  

for(i in 1:lat){  
    for(j in 1:long){  
        temp[counter,]<-get.var.ncdf(precipcd, varid= "Prcp", count = c(1,1,time,1), start=c(j,i,1,1))  
        counter<- counter+1  
    }  
}  

temp_gcm <- cbind(lat_c, long_c, temp)`

所以现在的问题是如何从“temp_gcm”中删除与“coord”中的纬度/经度数据对相对应的值?

4

2 回答 2

2

不,

我可以想到很多方法可以做到这一点。最简单但不是最有效的方法是使用 R 的which()函数,该函数接受一个逻辑参数,同时迭代要应用匹配的数据帧。当然,这是假设在更大的数据集中最多只能有一个匹配项。根据您的数据集,我会做这样的事情:

attach(temp_gcm)    # adds the temp_gcm column names to the global namespace
attach(coord)    # adds the coord column names to the global namespace

matched.temp = vector(length = nrow(coord)) # To store matching results
for (i in seq(coord)) {

   matched.temp[i] = temp[which(lat_c == coord1[i] & long_c == coord2[i])]
}

# Now add the results column to the coord data frame (indexes match)
coord$temperature = matched.temp

该函数返回数据帧中满足和匹配which(lat_c == coord1[i] & long_c == coord2[i])的所有行的向量,并且分别来自迭代中的行(注意:我假设这个向量的长度只有 1,即只有 1 个可能的匹配项)。然后将从满足逻辑条件的数据框中的列中分配值。请注意,这样做的目标是我们创建一个向量,该向量具有通过索引对应于数据帧行的匹配值。temp_gcmlat_clong_ccoord1coord2imatched.temp[i]temptemp_gcmcoord

我希望这有帮助。请注意,这是一种基本方法,我建议您查找函数merge()apply()以更简洁的方式执行此操作。

于 2012-11-02T22:50:30.053 回答
0

我添加了一个充满零的附加列,用作 IF 语句的结果。“x”是 temp_gcm 中的行数。“y”是列数(代表时间步长)。“temp_s”是标准化的温度数据

indicator<- matrix(0, nrow = x, ncol = 1)

precip_s<- cbind(precip_s, indicator)

temp_s<- cbind(temp_s, indicator)

for(aa in 1:x){

    current_lat<-latitudes[aa,1] #Latitudes corresponding to larger area

    current_long<- longitudes[aa,1] #Longitudes corresponding to larger area

    for(ab in 1:lenx){ #Lenx coresponds to nrow(coord)

        if(current_lat == coord[ab,1] & current_long == coord[ab,2]) {
            precip_s[aa,(y/12+1)]<-1 #y/12+1 corresponds to "indicator column"
            temp_s[aa,(y/12+1)]<-1
        } 
    }
}


precip_s<- precip_s[precip_s[,(y/12+1)]>0,] #Removes rows with "0"s remaining in "indcator" column

temp_s<- temp_s[temp_s[,(y/12+1)]>0,]


precip_s<- precip_s[,-(y/12+1)] #Removes "indicator column

temp_s<- temp_s[,-(y/12+1)]
于 2012-11-02T23:27:43.950 回答