简体   繁体   English

根据另一列中不同行中的值创建数据框或tibble

[英]create a dataframe or tibble based on values in different rows in another column

I have a column of event time offsets (ms) like this (but much bigger) 我有一列这样的事件时间偏移(ms)(但更大)

> ts_data = tibble( t = c(34, 78, 111, 165, 189))
> ts_data
# A tibble: 5 x 1
      t
  <dbl>
1    34
2    78
3   111
4   165
5   189

and I'd like to create a second column where the value in each row is the difference between the current row and the previous one (assuming t=0 at the start). 我想创建第二列,其中每行的值是当前行和前一行之间的差异(假设开始时t = 0)。 So (by hand) for the above data I want to end up with this .. 所以(手动)对于上面的数据我想最终得到这个..

> add_column(ts_data, t_int = c(34, 44, 33, 54, 24))
# A tibble: 5 x 2
      t t_int
  <dbl> <dbl>
1    34    34
2    78    44
3   111    33
4   165    54
5   189    24

ie 44 = 78-34; 即44 = 78-34; 33 = 111-78,... 33 = 111-78,......

I could do something with a loop but was sort of expecting that there might be a neater way using relative indexing however my quest to date has yet to bear fruit. 我可以用循环做一些事情,但有点期待可能有一种更简洁的方式使用相对索引,但是我对日期的追求尚未见效。

Any pointers would be appreciated :-) 任何指针将不胜感激:-)

An easier option with diff which returns a vector of length one less than the original vector (or column). 用更容易的选择diff它返回一个vectorlength少一个比原来的vector (或列)。 So, append the first value of 't' to create the length equal as that of the original column 因此,附加't'的first值来创建与原始列相等的length

library(dplyr)
ts_data %>% 
   mutate(t_int = c(first(t), diff(t)))
# A tibble: 5 x 2
#      t t_int
#  <dbl> <dbl>
#1    34    34
#2    78    44
#3   111    33
#4   165    54
#5   189    24

Or take the difference of the original column with the lag of the column specifying the default as 0 (by default it is NA ) 或者将原始列的差异与列的lag指定为default 0(默认情况下为NA

ts_data %>%
      mutate(t_int = t - lag(t, default = 0))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM