我的数据框架基于0.25度的数据集,由纬度、经度和相关温度组成。现在我想把分辨率从0.25改成0.5。例如,我的数据框的纬度和经度是70.5,70.25,70,69.75,69.5...,现在我只需要整数和小数部分0.5的坐标,比如70.5,70,69.5,69...How我能很容易做到吗?
发布于 2021-05-18 05:43:26
我们可以从plyr使用round_any
library(plyr)
unrounded <- c(runif(10)*10)
> unrounded
[1] 9.796907 4.237637 4.758592 1.109172 5.037765 3.077775 7.616236 3.872094
[9] 3.471238 8.831574
rounded <- round_any(unrounded, 0.5)
> rounded
[1] 10.0 4.0 5.0 1.0 5.0 3.0 7.5 4.0 3.5 9.0作为一个data.frame,你必须把它包装回一个data.frame中
unrounded2 <- data.frame(x = c(runif(10)*10))
> unrounded2
x
1 6.1078737
2 1.8496701
3 3.5469245
4 9.7893189
5 0.5503520
6 8.4338650
7 2.5316328
8 0.1954177
9 4.0447613
10 7.9741839
rounded2 <- data.frame(x= round_any(unrounded2$x, 0.5))
> rounded2
x
1 6.0
2 2.0
3 3.5
4 10.0
5 0.5
6 8.5
7 2.5
8 0.0
9 4.0
10 8.0发布于 2021-05-18 07:31:56
您可以通过先乘后除来舍入到0.5。
set.seed(1)
x <- c(runif(10)*10)
x
# [1] 2.6550866 3.7212390 5.7285336 9.0820779 2.0168193 8.9838968 9.4467527 6.6079779 6.2911404 0.6178627
round(x * 2)/2
# [1] 2.5 3.5 5.5 9.0 2.0 9.0 9.5 6.5 6.5 0.5作为data.frame的一部分
d <- data.frame(lon=x)
d$lon <- round(d$lon * 2) / 2https://stackoverflow.com/questions/67577118
复制相似问题