根据阈值对数据进行分类?

时间:2014-01-24 04:35:40

标签: r

假设我的响应变量随着时间的推移而上升和下降。每当响应变量超过阈值时,我们就会有一个新的“试验”。也就是说,如果我在高于特定值时添加ThresholdTRUE,则ThresholdTRUE的连续数据点块构成新的审判。

Time <- seq(1, 10, by = 0.5)
Response <- abs(sin(Time))
Threshold <- Response > 0.6
data <- data.frame(Time, Response, Threshold)

鉴于TimeResponseThreshold,如何添加Trial因素,为每组TRUE添加新值门槛?像这样:

   Time   Response Threshold Trial
1   1.0 0.84147098      TRUE A
2   1.5 0.99749499      TRUE A
3   2.0 0.90929743      TRUE A
4   2.5 0.59847214     FALSE NA
5   3.0 0.14112001     FALSE NA
6   3.5 0.35078323     FALSE NA
7   4.0 0.75680250      TRUE B
8   4.5 0.97753012      TRUE B
9   5.0 0.95892427      TRUE B
10  5.5 0.70554033      TRUE B
11  6.0 0.27941550     FALSE NA
12  6.5 0.21511999     FALSE NA
13  7.0 0.65698660      TRUE C
14  7.5 0.93799998      TRUE C
15  8.0 0.98935825      TRUE C
16  8.5 0.79848711      TRUE C
17  9.0 0.41211849     FALSE NA
18  9.5 0.07515112     FALSE NA
19 10.0 0.54402111     FALSE NA

2 个答案:

答案 0 :(得分:3)

data$Trial <- factor(
  ifelse(data$Threshold, cumsum(!data$Threshold), NA), labels = c("A", "B", "C")
)

##   Time   Response Threshold Trial
## 1   1.0 0.84147098      TRUE     A
## 2   1.5 0.99749499      TRUE     A
## 3   2.0 0.90929743      TRUE     A
## 4   2.5 0.59847214     FALSE  <NA>
## 5   3.0 0.14112001     FALSE  <NA>
## 6   3.5 0.35078323     FALSE  <NA>
## 7   4.0 0.75680250      TRUE     B
## 8   4.5 0.97753012      TRUE     B
## 9   5.0 0.95892427      TRUE     B
## 10  5.5 0.70554033      TRUE     B
## 11  6.0 0.27941550     FALSE  <NA>
## 12  6.5 0.21511999     FALSE  <NA>
## 13  7.0 0.65698660      TRUE     C
## 14  7.5 0.93799998      TRUE     C
## 15  8.0 0.98935825      TRUE     C
## 16  8.5 0.79848711      TRUE     C
## 17  9.0 0.41211849     FALSE  <NA>
## 18  9.5 0.07515112     FALSE  <NA>
## 19 10.0 0.54402111     FALSE  <NA>

答案 1 :(得分:2)

使用rle的另一种可能性:

r <- with(data, rle(Threshold))
len <- with(r, lengths[values])
n <- length(len)

trial <- rep(x = LETTERS[1:n], times = len)

data$Trial[data$Threshold] <- trial

data