【问题标题】:how to write loop statement when we have multiple variable and condition当我们有多个变量和条件时如何编写循环语句
【发布时间】:2020-01-01 15:04:33
【问题描述】:

如果我们有 latitude 和 longitude ,我需要找到一对之间的距离。还有条件如果任何变量为空,那么我们的结果应该是Null,如果我们有值den,它应该给出距离..如何为相同的创建循环?

Pairs   Latitude Longitude

16285   50.0354 19.2343
16285   50.0748 19.9125
16283       
16283       
16281       
16281       
16279       
16279       
16277       
16277       
16275       
16275       
16273       
16273       
16271   51.7549 19.463
16271   50.5908 21.1178
16269   33.5912 130.4013
16269   34.8234 134.8785



Pairs   Latitude    Longitude    distance

16285   50.0354 19.2343               500

16285   50.0748 19.9125               500
16283       
16283       
16281       
16281       
16279       
16279       
16277       
16277       
16275       
16275       
16273       
16273       
16271   51.7549 19.463              200
16271   50.5908 21.1178             200
16269   33.5912 130.4013             100
16269   34.8234 134.8785             100

如何通过循环实现这一点


标题

lat1 = as.numeric(fd2[3,3]) lat2 = as.numeric(fd2[4,3]) 
long1  =as.numeric(fd2[3,4]) 
long2 =as.numeric(fd2[4,4])

 earth.dist <- function (long1, lat1, long2, lat2) {   rad <- pi/180  
 a1 <- lat1 * rad   
a2 <- long1 * rad   
b1 <- lat2 * rad   
b2 <- long2 * rad   
dlon <- b2 - a2   
dlat <- b1 - a1   
a <- (sin(dlat/2))^2 + cos(a1) * cos(b1) * (sin(dlon/2))^2   
c <- 2 * atan2(sqrt(a), sqrt(1 - a))   
R <- 6378.145   
d <- R * c   
return(d) }

dist_km <- earth.dist(long1,lat1,long2,lat2) 
dist_mi <- dist_km / 1.609344

myout <- data.frame("dist_km" = dist_km, "dist_mi" = dist_mi)

条件: 检查对是否相等 如果存在任何值,则关联的 Latitude 和 Longitude 不应为 null ,那么它应该显示 null 。如果所有条件 满足然后是的。

【问题讨论】:

标签: r loops conditional-statements


【解决方案1】:

我认为“答案”是首先要了解在 R 数值向量中不存在完全命名为 Null 的值。您需要了解NA 将达到您所称的Null 的目的。目前尚不清楚您的数据采用什么形式,但如果您的 LatitudeLongitude 列属于“字符”类,那么 as.numeric 会将任何空白字符值转换为 NA 值.那么任何涉及 NA 值的算术计算都会自动产生 NA 结果。因此,R 的运作方式和您的目标不会有任何问题。

dat <- read.table(text="Pairs   Latitude Longitude
16285   50.0354 19.2343
16285   50.0748 19.9125
16283       
16283       
16281       
16281       
16279       
16279       
16277       
16277       
16275       
16275       
16273       
16273       
16271   51.7549 19.463
16271   50.5908 21.1178
16269   33.5912 130.4013
16269   34.8234 134.8785
", header =TRUE, fill=TRUE)

这实际上为您提供了所有数字列,因此不需要 as.numeric 操作(尽管它们也不会造成任何伤害。)。

 str(dat)
'data.frame':   18 obs. of  3 variables:
 $ Pairs    : int  16285 16285 16283 16283 16281 16281 16279 16279 16277 16277 ...
 $ Latitude : num  50 50.1 NA NA NA ...
 $ Longitude: num  19.2 19.9 NA NA NA ...

然后我们需要将您的功能代码放入可以剪切并粘贴到控制台会话中的表单中,我通过编辑您的问题正文完成了该操作。对其行为进行一些测试。

前两行并不是特别接近,因此不清楚您对该距离列中的距离的期望:

> earth.dist(dat[1,2], dat[1,3],dat[2,2],dat[2,3])
[1] 75.60999
> earth.dist(dat[1,2], dat[1,3],dat[1,2],dat[1,3])
[1] 0

我尚未检查 d 算法的正确性,但它似乎为 1 度的纬度差异提供了合理的值:

 earth.dist(1, 1,1,2)
[1] 111.3196

看来您可能想要测试连续行是否彼此接近,在这种情况下,使用 all.equal 函数将允许测试,当使用“tolerance”值在点接近时返回 TRUE 但在数值上不完全相等。

【讨论】:

    猜你喜欢
    • 2021-10-04
    • 1970-01-01
    • 1970-01-01
    • 2016-10-02
    • 2015-07-25
    • 2017-08-13
    • 1970-01-01
    • 2018-07-02
    • 1970-01-01
    相关资源
    最近更新 更多