根据条件拆分我的数据

时间:2017-10-07 16:27:02

标签: r subset

我有一个数据被分成两组,例如我需要一组中低于146的数据点和另一组中高于146的数据点。原始数据集是

dat <- c(208.3, 357.63, 238.06, 227.91, 231.79, 275.43, 241.27, 163.39, 
         160.31, 255.23, 214.74, 209.92, 160.32, 222.22, 133.8, 240.94, 
         147.46, 251.53, 165.07, 129.26, 149.4, 143.79, 249.73, 183.93, 
         252.03, 288.49, 208.29, 297.48, 208.97, 378.35, 153.96, 134.6, 
         370, 278.91, 290.48, 241.74, 224.41, 402.15, 284.57, 370.73, 
         266.67, 408.67, 164.09, 234.94, 206.94, 363.85, 208.11, 327.58, 
         213.55, 241.04, 216.4, 213.98, 276.65, 165.56, 243.86, 239.57, 
         266.16, 195.99, 185.05, 309.52, 399.34, 296.51, 192.39, 293.18, 
         212.11, 273.34, 134.18, 258.13, 221.16, 313.82, 214.37, 303.43, 
         233.79, 225.39, 211.06, 266.59, 177.92, 205.18, 190.18, 288.33, 
         207.09, 306.67, 240.01, 219.11, 195.42, 333.76, 190.28, 236.32, 
         214.03, 264.94, 228.61, 226.3, 206.24, 253.57, 233.92, 340.87, 
         217.67, 253.01, 209.19, 265.51)

3 个答案:

答案 0 :(得分:1)

data <- 136:156
set1 <- data[data<146]
set2 <- data[data>146]

答案 1 :(得分:1)

如果您的数据只是一个向量(从上面看起来如此):

# Example data
your_data <- seq(from = 1, to = 300, by = 3)

# Vector of data below the threshold 
below_146 <- your_data[which(your_data < 146)]

# Vector of data above the threshold 
above_146 <- your_data[which(your_data > 146)]

请注意,两个子集都不会包含完全相等的值。

答案 2 :(得分:1)

您可以将它们列入清单:

lst <- split(dat, dat<146)