【问题标题】:Cut Function in R programR程序中的剪切函数
【发布时间】:2013-03-26 02:26:49
【问题描述】:
Time    Velocity
0   0
1.5 1.21
3   1.26
4.5 1.31
6   1.36
7.5 1.41
9   1.46
10.5    1.51
12  1.56
13  1.61
14  1.66
15  1.71
16  1.76
17  1.81
18  1.86
19  1.91
20  1.96
21  2.01
22.5    2.06
24  2.11
25.5    2.16
27  2.21
28.5    2.26
30  2.31
31.5    2.36
33  2.41
34.5    2.4223
36  2.4323

所以我有关于时间和速度的数据...我想使用 cut 或 which 函数将我的数据分成 6 分钟的间隔...我的最大时间通常会达到 3000 分钟 所以我希望输出类似于这个......

Time    Velocity
0   0
1.5 1.21
3   1.26
4.5 1.31
6   1.36
Time    Velocity
6   1.36
7.5 1.41
9   1.46
10.5    1.51
12  1.56
    
Time    Velocity
12  1.56
13  1.61
14  1.66
15  1.71
16  1.76
17  1.81
18  1.86

所以到目前为止我所做的是使用 data=read.delim("clipboard") 读取数据 我决定使用函数'which'....但我需要做 3000 分钟等

dat <- data[which(data$Time>=0
& data$Time < 6),],
dat1 <- data[which(data$Time>=6
& data$Time < 12),]

等 但是如果我有时间去 3000 分钟,这不会那么方便 另外我希望我的所有结果都包含在一个输出/变量中

【问题讨论】:

  • 您确定要在两个间隔内重复 time = 6, 12, 18 ... 的值吗?
  • 要将数据分成这些组,要使用的 R 函数是 cut。如果你能解释为什么你会选择使用重复的逻辑切割,我全都听好了。此外,在这种情况下,您不需要 which。只要您聪明地处理丢失的数据,data[data$Time &gt;= 6 &amp; data$Time &lt; 12,] 就会起作用。
  • 我想使用 cut 但我不知道如何...是不是像 dat
  • @GabbyRinnsleigh 您没有回答是否要重复边界?

标签: r cut


【解决方案1】:

我在这里假设您真的不想在 bin 之间重复值。

cuts = cut(data$Time, seq(0, max(data$Time)+6, by=6), right=FALSE)
x <- by(data, cuts, FUN=I)

x
## cuts: [0,6)
##   Time Velocity
## 1  0.0     0.00
## 2  1.5     1.21
## 3  3.0     1.26
## 4  4.5     1.31
## ------------------------------------------------------------------------------------------------------------ 
## cuts: [6,12)
##   Time Velocity
## 5  6.0     1.36
## 6  7.5     1.41
## 7  9.0     1.46
## 8 10.5     1.51
## ------------------------------------------------------------------------------------------------------------ 
## <snip>
## ------------------------------------------------------------------------------------------------------------ 
## cuts: [36,42)
##    Time Velocity
## 28   36   2.4323

【讨论】:

    【解决方案2】:

    我不认为你想得到重复的界限。这里是一个不使用cut的简单解决方案(类似于@Mathew 解决方案)。

      dat <- transform(dat, index = dat$Time %/% 6)
      by(dat,dat$index,FUN=I)
    

    【讨论】:

      【解决方案3】:

      如果您确实需要重复的时间戳是 6 的整数倍,那么您必须在拆分之前进行一些数据复制。

      txt <- "Time    Velocity\n0   0\n1.5 1.21\n3   1.26\n4.5 1.31\n6   1.36\n7.5 1.41\n9   1.46\n10.5    1.51\n12  1.56\n13  1.61\n14  1.66\n15  1.71\n16  1.76\n17  1.81\n18  1.86\n19  1.91\n20  1.96\n21  2.01\n22.5    2.06\n24  2.11\n25.5    2.16\n27  2.21\n28.5    2.26\n30  2.31\n31.5    2.36\n33  2.41\n34.5    2.4223\n36  2.4323"
      
      DF <- read.table(text = txt, header = TRUE)
      
      # Create duplicate timestamps where timestamp is multiple of 6 second
      posinc <- DF[DF$Time%%6 == 0, ]
      neginc <- DF[DF$Time%%6 == 0, ]
      posinc <- posinc[-1, ]
      neginc <- neginc[-1, ]
      
      # Add tiny +ve and -ve increments to these duplicated timestamps
      posinc$Time <- posinc$Time + 0.01
      neginc$Time <- neginc$Time - 0.01
      
      # Bind original dataframe without 6 sec multiple timestamp with above duplicated timestamps
      DF2 <- do.call(rbind, list(DF[!DF$Time%%6 == 0, ], posinc, neginc))
      
      # Order by timestamp
      DF2 <- DF2[order(DF2$Time), ]
      
      # Split the dataframe by quotient of timestamp divided by 6
      SL <- split(DF2, DF2$Time%/%6)
      
      # Round back up the timestamps of split data to 1 decimal place
      RESULT <- lapply(SL, function(x) {
          x$Time <- round(x$Time, 1)
          return(x)
      })
      
      RESULT
      ## $`0`
      ##    Time Velocity
      ## 2   1.5     1.21
      ## 3   3.0     1.26
      ## 4   4.5     1.31
      ## 51  6.0     1.36
      ## 
      ## $`1`
      ##    Time Velocity
      ## 5   6.0     1.36
      ## 6   7.5     1.41
      ## 7   9.0     1.46
      ## 8  10.5     1.51
      ## 91 12.0     1.56
      ## 
      ## $`2`
      ##     Time Velocity
      ## 9     12     1.56
      ## 10    13     1.61
      ## 11    14     1.66
      ## 12    15     1.71
      ## 13    16     1.76
      ## 14    17     1.81
      ## 151   18     1.86
      ## 
      ## $`3`
      ##     Time Velocity
      ## 15  18.0     1.86
      ## 16  19.0     1.91
      ## 17  20.0     1.96
      ## 18  21.0     2.01
      ## 19  22.5     2.06
      ## 201 24.0     2.11
      ## 
      ## $`4`
      ##     Time Velocity
      ## 20  24.0     2.11
      ## 21  25.5     2.16
      ## 22  27.0     2.21
      ## 23  28.5     2.26
      ## 241 30.0     2.31
      ## 
      ## $`5`
      ##     Time Velocity
      ## 24  30.0   2.3100
      ## 25  31.5   2.3600
      ## 26  33.0   2.4100
      ## 27  34.5   2.4223
      ## 281 36.0   2.4323
      ## 
      ## $`6`
      ##    Time Velocity
      ## 28   36   2.4323
      ## 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-08
        • 2014-06-10
        • 1970-01-01
        • 1970-01-01
        • 2011-04-07
        • 1970-01-01
        相关资源
        最近更新 更多