我有一个可用的mapply函数 - 如何将其转换为mlply?

时间:2016-03-30 12:32:29

标签: r algorithm progress-bar plyr mapply

我编写了以下R代码,它将最近的邮政编码分配给一组北/东坐标:

<?php
$vehicles = array("saab"=>"Saab","fiat"=>"Fiat","audi"=>"Audi");
$count  = count_answers_belongToOne_question($questionNumber);
array_splice($vehicles , $count);
?>
<select class="form-control multiplechose_questionTypes" name="selector[]" id="selector" > 
   <option value="" disabled selected>Select your option</option>
   <?php
       foreach ($vehicles as $key => $value) {
    ?>
        <option value="<?php echo $key;?>"><?php echo $value;?></option>            
   <?php
       }
   ?>   
 </select>

由于我有超过500,000(x1,y1)坐标和超过1,000,000(x2,y2)坐标,这个mapply函数需要很长时间才能运行,我想监控进度。我知道mlply有一个进度条功能,但我无法运行它。我所做的是:

# Set of northing / easting coordinates that I need to assign a postcode to
x1 <- c(1,2,4,6,7)
y1 <- c(5,2,4,7,8)

# Postcode with northing / easting coordinates
postcode <- c("Postcode A", "Postcode B", "Postcode C", "Postcode D")
x2 <- c(5,3,4,2)
y2 <- c(8,1,2,4)

# Function that attributes closest postcode to (x1, y1) coordinates
algo <- function(x, y)
{
        dist <- which.min(sqrt(((x2 - x)^2) + ((y2 - y)^2)))
}

# mapply to run the function, and find the closest coordinates
postcode[mapply(algo, x1, y1, SIMPLIFY = T)]
[1] "Postcode D" "Postcode B" "Postcode C" "Postcode A" "Postcode A"

我做错了什么?将欣赏mlply(或其他m * ply函数)的正确R代码,并解释上述原因不正确的原因。

非常感谢您的时间和关注。

1 个答案:

答案 0 :(得分:3)

我发现了至少两个问题。

首先,数据框中列的名称与函数中参数的名称不匹配。以下代码无需警告即可运行。

mlply(cbind(x= x1, y =y1), .fun = algo, .progress = "tk")

第二次,mlply返回一个列表,其中的元素不能用于对邮政编码向量进行子集化:

mlply(.data = cbind(x = x1, y = y1), .fun = algo, .progress = "tk")

$ 1 [1] 4

$ 2 [1] 2

$ 3 [1] 3

$ 4 [1] 1

$ 5 [1] 1

ATTR( “split_type”) [1]“数组” ATTR( “split_labels”)   x y 1 1 5 2 2 2 3 4 4 4 6 7 5 7 8

要解决此问题,我建议:

postcode[unlist(mlply(.data = cbind(x = x1, y = y1), 
    .fun = algo, .progress = "tk"))[1:length(x1)]]

最后,如果你试图寻找最小距离,你可以考虑直接寻找最小平方距离(你避免计算平方根一百万次,这应该会改善时间)。