[英]ifelse statement inside for loop R
I am currently working with a very large dataset of elevations for which I need to attribute certain temperatures for each row based on binned elevation in 10m intervals. 我目前正在使用一个非常大的海拔数据集,我需要基于10m间隔的合并海拔高度为每行分配某些温度。 Because the dataset is so large, I cannot write a simple loop. 因为数据集太大,所以我无法编写简单的循环。 I've created a smaller, fake dataset to demonstrate what I am trying to accomplish. 我创建了一个较小的假数据集,以演示我要完成的工作。
elevation <- seq(0, 100, 10)
e <- runif(n=100,min=0,max=100)
lettertype <- c(rep("a", 25), rep("b", 25), rep("c", 50))
x <- data.frame(e, lettertype)
Winter <- runif(n=10, min=10, max=15)
Spring <- runif(n=10, min=12, max=16)
Summer <- runif(n=10, min=14, max=20)
Fall <- runif(n=10, min=11, max=16)
elevbin <- c("0 to 10", "10 to 20", "20 to 30", "30 to 40", "40 to 50",
"50 to 60", "60 to 70", "70 to 80", "80 to 90", "90 to 100")
y <- data.frame(elevbin, Winter, Spring, Summer, Fall)
Wintertemp <- c()
for (i in 1:length(y$Winter)){
for (j in 1:length(elevation)){
for (k in 1:length(x$e)){
Wintertemp <- c(Wintertemp, (ifelse(x$e[k] >= elevation[j] & x$e < elevation[j+1], y$Winter[i], NA)))
}
}
}
Essentially elevation is a vector which will tell the for loop the elevations within each bin (again this must be automated due to the size of the dataset, spanning 200 elevation bins). 本质上,elevation是一个向量,它将告诉for循环每个bin中的海拔(同样,由于数据集的大小,它必须是自动化的,跨越200个height bins)。
The dataframe x is an example of the raw elevation data I had, the column "lettertype" is not important, it was just an add-on to turn the vector e into a dataframe. 数据框x是我拥有的原始高程数据的示例,列“ lettertype”并不重要,它只是将矢量e转换为数据框的附加组件。
The dataframe y has the information on which temperatures (by season, in this example I only use Winter but I need to do this for each season) are represented in each elevation bin. 数据框y包含有关每个海拔箱中代表哪些温度的信息(按季节,在此示例中,我仅使用“冬季”,但每个季节都需要这样做)。
I want to cbind the output to the dataframe x 我想将输出绑定到数据框x
For example: 例如:
head(x)
e lettertype
1 5.801268 a
2 99.115869 a
3 31.928297 a
4 25.886745 a
5 93.288743 a
6 23.496359 a
y
elevbin Winter
1 0 to 10 12.51486
2 10 to 20 14.14977
3 20 to 30 10.19768
4 30 to 40 12.63925
5 40 to 50 10.18445
6 50 to 60 10.91821
7 60 to 70 13.00326
8 70 to 80 11.89128
9 80 to 90 13.88241
10 90 to 100 12.33358
So I would like my outcome to be: 所以我希望我的结果是:
head(x)
e lettertype Winter
1 5.801268 a 12.51486
2 99.115869 a 12.33358
3 31.928297 a 12.63925
4 25.886745 a 10.19768
5 93.288743 a 12.33358
6 23.496359 a 10.19768
In simpler terms I want to assign the correct temperature for each elevation(e) in dataframe x, based on the temperatures from the elevation bins. 简单来说,我想根据海拔箱中的温度为数据框x中的每个海拔分配正确的温度。
Currently the code returns a vector of length 1100000 with NAs and the first number in the winter vector, which is not correct. 当前,该代码返回一个长度为1100000的矢量,其中包含NA和冬季矢量中的第一个数字,这是不正确的。 The outcome should be the same length as the x dataframe. 结果应与x数据帧的长度相同。
Any help would be great! 任何帮助将是巨大的! Thanks in advance! 提前致谢!
There is no need for a for
loop or ifelse
. 不需要for
循环或ifelse
。 You can merge
the two data frames if you create appropriate columns first: 如果先创建适当的列,则可以merge
两个数据框:
elevation <- seq(0, 100, 10)
e <- c(seq(0, 99, 1))
elev_floor <- 10 * floor(e / 10)
x <- data.frame(e, elev_floor)
Winter <- runif(n=10, min=10, max=15)
Spring <- runif(n=10, min=12, max=16)
Summer <- runif(n=10, min=14, max=20)
Fall <- runif(n=10, min=11, max=16)
elevbin <- c("0 to 10", "10 to 20", "20 to 30", "30 to 40", "40 to 50",
"50 to 60", "60 to 70", "70 to 80", "80 to 90", "90 to 100")
elev_floor <- seq(0, 90, 10)
y <- data.frame(elevbin, elevmin, Winter, Spring, Summer, Fall)
merge(x, y)
Result: 结果:
e elev_floor elevbin elevmin Winter Spring Summer Fall
1 0 0 0 to 10 0 12.53781 13.01907 16.74989 15.22208
2 1 0 0 to 10 0 12.53781 13.01907 16.74989 15.22208
3 2 0 0 to 10 0 12.53781 13.01907 16.74989 15.22208
4 3 0 0 to 10 0 12.53781 13.01907 16.74989 15.22208
5 4 0 0 to 10 0 12.53781 13.01907 16.74989 15.22208
6 5 0 0 to 10 0 12.53781 13.01907 16.74989 15.22208
7 6 0 0 to 10 0 12.53781 13.01907 16.74989 15.22208
8 7 0 0 to 10 0 12.53781 13.01907 16.74989 15.22208
9 8 0 0 to 10 0 12.53781 13.01907 16.74989 15.22208
10 9 0 0 to 10 0 12.53781 13.01907 16.74989 15.22208
11 10 10 0 to 10 0 12.53781 13.01907 16.74989 15.22208
12 11 10 0 to 10 0 12.53781 13.01907 16.74989 15.22208
13 12 10 0 to 10 0 12.53781 13.01907 16.74989 15.22208
14 13 10 0 to 10 0 12.53781 13.01907 16.74989 15.22208
If your bins are more complicated than in your example, than you can adjust the calculation of x$elev_floor
. 如果您的x$elev_floor
比示例中的要复杂,则可以调整x$elev_floor
的计算。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.