插入深度测量中的步骤

时间:2017-04-23 12:48:53

标签: r

假设我从线性测量设备获得以下数据:

x <- c(0,0,0,0,1,1,1,1,1,2,2,2,2,2,3,3,4,4,5,6,7,8,9,9)

使用diff()计算滞后和迭代的差异将最终得到:

c(0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,1,0,1,1,1,1,1,0)

这不是我要找的。 有没有办法插入深度以平滑步骤:

c(0,0.25,0.5,0.75,1,1.2,1.4,1.6,1.8,2,2.2,2.4,2.6,2.8,3,3.5,4,4.5,5,6,7,8,9,9....)

注意:数据与开头的数据相同,只是将步骤插值为连续增加。 这样做的原因是设备的分辨率不足以显示介于两者之间的步骤但是随着时间的推移两次变化之间的持续增加绰绰有余然后采取diff()来计算线性数据的速度测量装置。

5 个答案:

答案 0 :(得分:1)

这就是你想要的吗?

public static String ByteArrayToHexString(byte[] ba)
    {
        System.Text.StringBuilder hex = new StringBuilder(ba.Length * 2);
        foreach (byte b in ba)
        {
            hex.AppendFormat("{0:x2}", b);
        }
        return hex.ToString();
    }

    public String CreateSalt(int size) // Function to generate a random salt 
    {
        var rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
        var buff = new byte[size];
        rng.GetBytes(buff);
        return Convert.ToBase64String(buff);
    }

    public String GenerateSHA256Hash(String input, String salt) // Function to add user input and randomly generated salt
    {
        byte[] bytes = System.Text.Encoding.UTF8.GetBytes(input + salt);
        System.Security.Cryptography.SHA256Managed sha256hashstring = new System.Security.Cryptography.SHA256Managed();
        byte[] hash = sha256hashstring.ComputeHash(bytes);
        return ByteArrayToHexString(hash);
    }

答案 1 :(得分:1)

我们可以使用函数rle()查找每个值的游程长度,让我们称之为lens。现在,我们可以在1中计算每个连续值的lens除以base R的累积总和:

lens <- rle(x)$lengths
cumsum(c(0, head(rep(1/lens,lens),-1)))
#[1] 0.00 0.25 0.50 0.75 1.00 1.20 1.40 1.60 1.80 2.00 2.20 2.40 2.60 2.80 3.00 3.50 4.00 4.50 5.00 6.00
#[21] 7.00 8.00 9.00 9.50

答案 2 :(得分:0)

使用mapply的另一个建议。

> smoothFunc <- function(x) {
+   tab.x <- table(x) 
+   uni.x <- unique(x)
+   return(unlist(mapply(seq, from = uni.x, to = uni.x +  (tab.x - 1) / tab.x , length.out = tab.x)))
+ }
> 
> smoothFunc(x)
 [1] 0.00 0.25 0.50 0.75 1.00 1.20 1.40 1.60 1.80 2.00 2.20 2.40 2.60 2.80 3.00 3.50 4.00 4.50 5.00 6.00 7.00 8.00 9.00
[24] 9.50

答案 3 :(得分:0)

使用可以使用dplyr

library(dplyr)
x <- x %>% group_by(x) %>%
    mutate(a = 1/n(),
           b = lag(a, default = 0),
           c = cumsum(b),
           d = x+c)
x$d
# [1] 0.00 0.25 0.50 0.75 1.00 1.20 1.40 1.60 1.80 2.00 2.20 2.40 2.60 2.80 3.00 3.50 4.00
# [18] 4.50 5.00 6.00 7.00 8.00 9.00 9.50

答案 4 :(得分:0)

动态解决方案,如果您的运行之间的区别是&gt; 1。

library(data.table)
rle_x <- rle(x)
vec <- unlist(sapply(1:(length(rle_x$values) - 1), 
                     function(i, x, rle) {
                          shift <- ((x[i+1] - x[i])/rle[i])
                          seq(x[i], x[i+1] - (shift), by = shift)
                       },
              rle = rle_x$lengths, x = rle_x$values))

res <- c(vec, rep(rle_x$values[length(rle_x$values)], rle_x$lengths[length(rle_x$lengths)]))
res
 #[1] 0.00 0.25 0.50 0.75 1.00 1.20 1.40 1.60 1.80 2.00 2.20 2.40 2.60 2.80 3.00 3.50 4.00 4.50 5.00 6.00
#[21] 7.00 8.00 9.00 9.00

如果您的输入向量如下:x <- c(0,0,0,0,2,2,2,2,2,3,3,4,4,5,6,7,8,9,9)。结果将是:

#[1] 0.0 0.5 1.0 1.5 2.0 2.2 2.4 2.6 2.8 3.0 3.5 4.0 4.5 5.0 6.0 7.0 8.0 9.0 9.0