简体   繁体   English

估算后,如何舍入到因子的最近水平

[英]After imputation, how to round to nearest level of a factor

I have imputed missing values in the following data frame in column q1. q1 我在第q1. q1列的以下数据框中估算了缺少的值q1. q1 q1. q1 is a factor with the levels 0,20,40,60,80,100 . q1. q1是水平为0,20,40,60,80,100的因子。 I need to round the column q1 to nearest level of factors, number 49 and 91 need to be rounded. 我需要将q1列四舍五入到最接近的因子水平,数字49 and 91需要四舍五入。 Does anyone have a solution for this problem?, thanks in advance! 有人对此问题有解决方案吗?,谢谢!

id <- rep(c(300,450), each=6)
> visit <- rep(1:6,2)
> trt <- rep(c(0,"A",0,"B",0,"C"),2)
> q1 <- c(0,100,0,89,0, 60,0,85,0,40,0, 20)
> df <- data.frame(id,visit,trt,q1)
> df
    id visit trt  q1
1  300     1   0   0
2  300     2   A 100
3  300     3   0   0
4  300     4   B  49
5  300     5   0   0
6  300     6   C  60
7  450     1   0   0
8  450     2   A  91
9  450     3   0   0
10 450     4   B  40
11 450     5   0   0
12 450     6   C  20
>

Maybe you could use plyr 's round_any function here which would round to nearest multiple of 20 in this case. 也许您可以在此处使用plyrround_any函数,在这种情况下,该函数将舍入到最接近的20的倍数。

plyr::round_any(df$q1, 20)
#[1]   0 100   0  80   0  60   0  80   0  40   0  20

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM