检查是否意味着破坏某些限制R.

时间:2017-11-15 11:54:27

标签: r

我有一个100x2的数据集,第一列标识样本,第二列表示测量。每个样本的大小为4,因此数据集如下所示:

import java.util.*;
import java.lang.*;
import java.io.*;

class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        String s="anupam+singh"; /* here the string is composed of three parts*/
        String s1="anupam"; /*this is the first part of the string, in your case it is **retrieved name***/

        if(s.substring(0,s.indexOf("+")).equals(s1)) /*we use a substring function of String class, it takes two arguements starting and end index and returns the substring from the given string, the end index is exclusive*/
        {
            String s2=s.substring(s.indexOf("+")+1); /*the substring function can work on one arguement even, if you just give the starting index then it will return the substring from that starting index till the end of string */
            System.out.println(s2);
        }


    }
}

我使用以下方法计算了每个样本的平均值:

V1  V2
1   125.8
1   128.4
1   129.0
1   121.0
2   125.2
2   127.0
2   130.4
2   124.6
3   121.8
3   126.8
..  ..

所以我现在有一个25手段的向量。我想检查这些方法中的每一个是否超出某些限制(定义如下),然后返回一个包含样本ID的对象,样本的平均值,以及它破坏了哪个限制的信息,将输出设置为类“shewart”:

df2 = subset(data, select = c(2))
means =  t(sapply(split(df2,rep(seq(1,nrow(df2),4),each=4)),colMeans)) 

2 个答案:

答案 0 :(得分:1)

我稍微改变了您提供的值,以确保该过程能够正确找到您想要的模式。

library(dplyr)

# example dataset
df = read.table(text = "
V1  V2
1   195.8
1   128.4
1   129.0
1   121.0
2   125.2
2   127.0
2   125.4
2   114.6
", header=T)

# function to return info based on a value
f = function(x) {
  ifelse(x < 121.9, "below action",
         ifelse(x < 123.6, "below warning", 
                ifelse(x > 132.1, "above action",
                       ifelse(x > 130.4, "above warning", "normal"))))
  }


df %>%
  group_by(V1) %>%                      # for each V1
  summarise(MeanValue = mean(V2)) %>%   # calculate means
  mutate(info = f(MeanValue))           # use the function to get relevant info

# # A tibble: 2 x 3
#      V1 MeanValue          info
#   <int>     <dbl>         <chr>
# 1     1    143.55  above action
# 2     2    123.05 below warning

答案 1 :(得分:1)

当AntoniosK发布他的时候,我正在研究一个解决方案。

我还使用了dplyr包:

library(dplyr)
df1 <- read.table(text = "V1  V2
1   125.8
1   128.4
1   129.0
1   121.0
2   125.2
2   127.0
2   130.4
2   124.6
3   121.8
3   126.8
3   133.8
3   144.5", header = TRUE)

warn_upper <- 130.4
action_upper  <- 132.1
warn_lower <- 123.6
action_lower  <- 121.9

df1 %>%
        group_by(V1) %>%
        summarize(smean = mean(V2)) %>%
        mutate(warn_hi   = (smean - warn_upper) > 0 ,
               action_hi = (smean - action_upper ) > 0,
               warn_lo   = (-1 * (smean - warn_lower)) > 0 ,
               action_lo = (-1 * (smean - action_lower )) > 0) %>%
        mutate(klass = if_else(warn_lo | action_lo | 
                               warn_hi | action_hi, true = "shewart", false="" ))

结果:

     V1 smean warn_hi action_hi warn_lo action_lo  klass
  <int> <dbl>   <lgl>     <lgl>   <lgl>     <lgl>   <chr>
1     1 126.0   FALSE     FALSE   FALSE     FALSE        
2     2 126.8   FALSE     FALSE   FALSE     FALSE        
3     3 131.7    TRUE     FALSE   FALSE     FALSE shewart