我有两个数据框:“unit_test”,包含调查单位的唯一描述(每个调查单位一行)和“data_test”,包含字段数据(每个调查单位多行)。如果是地面调查(data_test $ type ='ground'),我想将data_test $ easting替换为unit_test $ east中相应代码的值(unit_test $ code必须匹配data_test $ code1)。如果是空中调查(data_test $ type =='air'),我想将原始值保存在data_test $ easting中。
if(data_test$type=="ground") {
data_test$easting <- unit_test$east[match(data_test$code1, unit_test$code)]
}
我尝试过使用匹配功能:
code1 type easting northing species count
1 pondA ground 12345 99876 NOPI 10
2 pondA ground 12345 99876 NOPI 23
3 transect1 air 18264 96022 SCAU 50
4 pondB ground 23456 98765 GWTE 1
5 pondB ground 23456 98765 GWTE 2
6 transect2 air 46378 85766 RUDU 43
7 pondC ground 34567 87654 NOPI 12
8 transect3 air 86025 21233 GADW 3
9 pondD ground 45678 76543 NOPI 7
10 transect4 air 46295 23090 MALL 9
然而,如果data_test $ type =='air'与NA,它会替换东方值。任何帮助将非常感激。
我希望我的最终输出看起来像这样:
function getElementsByClass(&$parentNode, $tagName, $className) {
$nodes=array();
$childNodeList = $parentNode->getElementsByTagName($tagName);
for ($i = 0; $i < $childNodeList->length; $i++) {
$temp = $childNodeList->item($i);
if (stripos($temp->getAttribute('class'), $className) !== false) {
$nodes[]=$temp;
}
}
return $nodes;
}
答案 0 :(得分:2)
我认为data.table
包对此任务非常有用:
install.packages("data.table")
library(data.table)
unit_test = data.table(unit_test)
data_test = data.table(data_test)
向unit_test
添加一列,指定它引用&#34; ground&#34;:
unit_test$type = "ground"
将键设置为表格以交叉引用
setkey(data_test, code1, type, species)
setkey(unit_test, code, type)
每次你有&#34;地面&#34;要在data_test
中输入内容,请在unit_test
中查找相应的数据,并将easting
替换为east
data_test[unit_test, easting:= east]
data_test[unit_test,northing:= north]
结果:
> data_test
code1 type easting northing species count
1: pondA ground 12345 99876 NOPI 10
2: pondA ground 12345 99876 NOPI 23
3: pondB ground 23456 98765 GWTE 1
4: pondB ground 23456 98765 GWTE 2
5: pondC ground 34567 87654 NOPI 12
6: pondD ground 45678 76543 NOPI 7
7: transect1 air 18264 96022 SCAU 50
8: transect2 air 46378 85766 RUDU 43
9: transect3 air 86025 21233 GADW 3
10: transect4 air 46295 23090 MALL 9
答案 1 :(得分:0)
基地R:
data_test[data_test$type == 'ground',c('easting','northing')] <- unit_test[match(data_test[data_test$type == 'ground','code1'],unit_test$code),c('east','north')]
找到你要填充的地点,然后像你提到的那样制作一个匹配的索引。这是在更改样本数据之后。我在创建两个数据帧时都使用stringsAsFactors = F,所以我没有必要处理因素。