从 R 中的指标变量获取值 [英] Getting values from indicator variables in R

查看:47
本文介绍了从 R 中的指标变量获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我必须对 20m X 30m 矩形中两点的坐标进行建模.显然这些点遵循均匀分布,所以这是我代码的第一部分:

Firstly I had to model the coordinates of two points in a 20m X 30m rectangle. Obviously these points follow a uniform distribution so this is the first part of my code:

    X1 <- runif(1,0,20)
    X2 <- runif(1,0,30)
    point1 <- c(X1,X2)
    point1

我对第二个点 ('point2') 使用了相同的代码,但分别用 Y1 和 Y2 替换了 X1 和 X2.

I used the same code for the second point ('point2') but replaced the X1 and X2 with Y1 and Y2 respectively.

然后我必须找到两点之间的距离:

I then had to find the distance between the two points:

    distance <- sqrt(((X1-Y1)^2)+((X2-Y2)^2))

现在,如果我将 A 定义为点之间的距离在 5m 到 10m 之间的事件,我需要找到该事件的指示变量.这就是我得到的,但我不确定它是否正确:

Now if I define A as the event where the points are within 5m to 10m of each other, I need to find the indicator variable of this event. This is what I got to, but I'm not sure if it's right:

    x=0
    if (distance < 10 & distance > 5)
    {
     x=1
    }
    Z <- c(distance,x)

如果我将这些命令重复 1000 次,我将如何在每次模拟中存储这些值并在 1000 次重复中找到最小和最大间隔值?

If I were to repeat these commands 1000 times, how would I store the values in each simulation and find the min and max separation values in the 1000 reps?

推荐答案

有一次你使用了 X1 和 X2,后来又引用了 Y1,但尚未定义.我很确定你想使用:

At one point you use X1 and X2 and later refer to Y1 which has not been defined. I'm pretty sure you want to use:

 points <- cbind(X1, X2)

上下文表明您希望将 X1 和 X2 值保持在平行"排列中,并且定义一个矩阵而不是一个无量纲的对象就可以实现这一点.

Context suggests that you wanted to keep the X1 and X2 values in a "parallel" arrangement and defining a matrix rather than a non-dimensioned object will accomplish that.

使用 R 的矩阵运算回答的最后一个问题:

Last question answered using R's matrix operations:

points1 <- matrix( runif(2000), ncol=2)
points1 <- matrix( runif(2000), ncol=2)
dists <- rowSums( (points1-points2)^2 )
Z <- dists <10 & dists >5

这篇关于从 R 中的指标变量获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆