根据R中的多个嵌套条件填写列

时间:2017-12-05 02:19:46

标签: r loops if-statement

我正在模拟作业的数据集,我需要帮助,我找了几个小时但找不到解决问题的方法。我会尽快解释一下:

我有两个变量:

  • 季节:4季(春,夏,秋,冬)。
  • 深度:5个深度范围(< 100 / 100-400 / 400-700 / 700-1000 /> 1000)

我想生成一个第三个变量,它依赖于那些独立的变量,这是一个丰富的海豚(泊松分布)。所以基本上,需要改变的是我的lambda。 到目前为止我发现的唯一方法就是做4次不同的矢量,每个季节一次。最后,将它们放在一起。

我的代码工作得很好,但它非常冗长可笑,我知道我可以缩短它!但我不知道如何...请帮助:D

提前致谢!!!

到目前为止我的代码:

abundance_summer <- rep(NA, length(bottom_depth))
set.seed(5)
for (i in 1:length(abundance_summer)){
    if (bottom_depth[i] < 100){
    abundance_summer[i] <- rpois(n=1, lambda=0)
  } else if (bottom_depth[i] > 100 & bottom_depth[i] < 400){
    abundance_summer[i] <- rpois(n=1, lambda=2)
  } else if (bottom_depth[i] > 400 & bottom_depth[i] < 700){
    abundance_summer[i] <- rpois(n=1, lambda=4)
  } else if (bottom_depth[i] > 700 & bottom_depth[i] < 1000){
    abundance_summer[i] <- rpois(n=1, lambda=6)
  } else if (bottom_depth[i] > 1000){
    abundance_summer[i] <- rpois(n=1, lambda=8)
  }
}

abundance_spring <- rep(NA, length(bottom_depth))
set.seed(5)
for (i in 1:length(abundance_spring)){
  if (bottom_depth[i] < 100){
    abundance_spring[i] <- rpois(n=1, lambda=2)
  } else if (bottom_depth[i] > 100 & bottom_depth[i] < 400){
    abundance_spring[i] <- rpois(n=1, lambda=6)
  } else if (bottom_depth[i] > 400 & bottom_depth[i] < 700){
    abundance_spring[i] <- rpois(n=1, lambda=13)
  } else if (bottom_depth[i] > 700 & bottom_depth[i] < 1000){
    abundance_spring[i] <- rpois(n=1, lambda=20)
  } else if (bottom_depth[i] > 1000){
    abundance_spring[i] <- rpois(n=1, lambda=19)
  }
}

abundance_autumn <- rep(NA, length(bottom_depth))
set.seed(5)
for (i in 1:length(abundance_autumn)){
  if (bottom_depth[i] < 100){
    abundance_autumn[i] <- rpois(n=1, lambda=1)
  } else if (bottom_depth[i] > 100 & bottom_depth[i] < 400){
    abundance_autumn[i] <- rpois(n=1, lambda=3)
  } else if (bottom_depth[i] > 400 & bottom_depth[i] < 700){
    abundance_autumn[i] <- rpois(n=1, lambda=6)
  } else if (bottom_depth[i] > 700 & bottom_depth[i] < 1000){
    abundance_autumn[i] <- rpois(n=1, lambda=4)
  } else if (bottom_depth[i] > 1000){
    abundance_autumn[i] <- rpois(n=1, lambda=8)
  }
}

abundance_winter <- rep(NA, length(bottom_depth))
set.seed(5)
for (i in 1:length(abundance_winter)){
  if (bottom_depth[i] < 100){
    abundance_winter[i] <- rpois(n=1, lambda=2)
  } else if (bottom_depth[i] > 100 & bottom_depth[i] < 400){
    abundance_winter[i] <- rpois(n=1, lambda=6)
  } else if (bottom_depth[i] > 400 & bottom_depth[i] < 700){
    abundance_winter[i] <- rpois(n=1, lambda=23)
  } else if (bottom_depth[i] > 700 & bottom_depth[i] < 1000){
    abundance_winter[i] <- rpois(n=1, lambda=16)
  } else if (bottom_depth[i] > 1000){
    abundance_winter[i] <- rpois(n=1, lambda=10)
  }
}

common_dolphins_abundance <- c(abundance_autumn, abundance_spring,abundance_summer, abundance_winter)

1 个答案:

答案 0 :(得分:2)

这个怎么样?

你不能给bottom_depth所以我会生成一些随机数。

set.seed(5);
bottom_depth <- runif(100, 0, 2000);

根据您的5个范围设置bottom_depth个值。

# Bin depth into 5 ranges
breaks.depth <- cut(bottom_depth, breaks = c(0, 100, 400, 700, 1000, max(bottom_depth)));

lambda参数存储在dataframe中,其中列对应于季节,行对应深度区。

# Dataframe of lambda parameters for every month for every depth bin
df.lambda <- data.frame(
    summer = c(0, 2, 4, 6, 8),
    spring = c(2, 6, 13, 20, 19),
    autumn = c(1, 3, 6, 4, 8),
    winter = c(2, 6, 23, 16, 10));

对于每个季节,生成泊松分布样本,每个lambda值一个样本:

abundance <- lapply(df.lambda, function(x) {
    rpois(length(bottom_depth), lambda = x[as.numeric(breaks.depth)])
})
#$summer
#  [1]  7 10  8  7  3  7  6  8  8  1  2  6  5  6  4  3  6  5  8  5  5 14  6  6  1
# [26]  2  7 10  1  7  5  1  0  0  0  8  7  2  3  5  7  4  7  6  7 12  9  3  8  9
# [51]  7  9  7  9 11  3  4  5  2  1  6  3  9  7  3 12 11  2  8  5 10 14  1 12  0
# [76] 15  0  3  5  6  8 14  3  0  8  3  5  8  5  4 11  2  3  4  5 10  3 12  5  5
#
#$spring
#  [1] 12 18 10 14  6 19 18 22 18  2 11 20 18 30 13  8 13 21 19 18 24 22 19  8  4
# [26] 23 22 20  3 14 21  4 10  6  1 24 17 17 20 18 12 17 14 16 10 24 17  9 18 22
# [51] 11 18 18 16 15 32 11 13  6  8 27  9 23 13 15 17 22  3 23 16 16 25  9 24  3
# [76] 22  5 16 15 11 23 15 12  7 22  8 20 22 14 17 16 11  8 16 12 25 14 21 20 19
#
#$autumn
#  [1] 11  4  7  1  1  4  7 13  3  2  5  4  4  7  0  3  3  3 11 10  6 14  4  6  2
# [26]  8  8 15  6 10  2  4  5  1  2  5  9  2  4  6  8  6 12  8  4  8  6  3  9 11
# [51]  3  7  5 13  9  3  5  4  2  3  8  0  7  9  3 12  4  1  6  6  6  6  4  4  1
# [76]  4  0  8  4  8 11  7  4  2  4  4 15 12  8  9  6  7  1  5  4  7  5  4  2  6
#
#$winter
#  [1] 19 14 12 21  3 10 10  9 10  4 22 23 22 12 26 28 17  7  9 12 11 10 23 19  7
# [26] 19 16 10  9  8 21  6 23  0  2 12 15 10 22 20  6 26  6 13 12  7  5  3  8 14
# [51] 17 12  7  9 15 10 19 23  4  6  8  6 11  7  7 11 10  0  9 11  8 13  6  7  1
# [76]  5  2 13  7 19  8  9 25 11 22  7  3 16 25 13  8 28  4 23 27 13 22 10 16 15