使用不同数据帧的内容操作数据帧 [英] Manipulating a data frame with contents from a different data frame
问题描述
我有一个大的 data.frame
与地理位置名称( mydata
)。这些地方出现在多个单元格的 data.frame
中。
I have a large data.frame
with geographic places names (mydata
). The places appear in the data.frame
in more than one cell.
在另一个包含3列的文件中,我拥有所有这些位置(第一列),这些位置的纬度(第二个地方出现在 data.frame
在多个单元格中)。在另一个包含3列的文件中,我拥有所有这些位置(第一列),这些位置的纬度(第二列)和第三列中的经度。
In an other file with 3 columns I have all these places (first column), the latitude of these places (second the places appear in the data.frame
in more than one cell). In an other file with 3 columns I have all these places (first column), the latitude of these places (second column) and the longitude in the third column.
I要创建尺寸等于
的另外两个矩阵( Lat
和 Long
), code> data.frame 与地理位置如此:
I want to create two other matrices (Lat
and Long
) with dimension equal to
the dimension of the data.frame
with the geographic places such that:
Lat[i,j] = Latitude of the place in mydata[i,j]
long[i,j]= longitude of the place in mydata[i,j]
所以我正在寻找一个通过 mydata
的过程,选择每个单元格中的位置名称,在第二个文件中查找纬度和经度,并将这些值填入矩阵 Lat
和 Long
。
So I am looking for a procedure that go through mydata
, pick the name of the place in each cell, look for the Latitude and Longitude in the second file and fill these values in the matrices Lat
and Long
.
mydata <- data.frame(cbind(c("xyz","ab","yabc",NA)),
c("xyz","xyz","yabc","ab")),
c("ab","ab",NA,"yabc")))
Coor <- data.frame(cbind(c("ab","xyz","yabc"),
c(31.34,42.15,36.98),
c(12.87,13.67,18.56)))
我想要一种方法来获取
Lat <- data.frame(cbind(c(42.15,31.34,36.98,NA),
c(42.15,42.15,36.98,31.34),
c(31.34,31.34,NA,36.98)))
推荐答案
像这样:
Lat<-do.call(cbind, lapply(mydata, function(curcol){ Coor[match(curcol, Coor[,1]), 2] }))
可以吗?
这篇关于使用不同数据帧的内容操作数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!