R programming: How can I compute the difference between two cells in a data frame and save them in a new column

时光总嘲笑我的痴心妄想 提交于 2019-12-12 08:09:05

问题


Trying to learn R and am stuck on an autocorrelation example. I want to regress the difference in x against the difference in y. I have x and y in a data frame, and would like the difference of x2 - x1 to be saved in a new column say dx. I have no idea how to go about this.

what I have:

data1

x   y
5   3
8   9
3   1
1   5
.   .
.   .
.   .

what I would like to get:

data1.dif

x   y   dx   dy
5   3   NA   NA
8   9    3    6
3   1   -5   -8
1   5   -2    4
.   .    .    .
.   .    .    .

回答1:


Use diff, and stick an NA to the beginning of the resulting vectors.

e.g.

data1 <- read.table(text='  x y
1 5 3
2 8 9
3 3 1
4 1 5')

# diff calculates the difference between consecutive pairs of 
#  vector elements
diff(data1$x)
[1]  3 -5 -2

# apply diff to each column of data1, bind an NA row to the beginning,
#  and bind the resulting columns to the original df
data1.dif <- cbind(data1, rbind(NA, apply(data1, 2, diff)))
names(data1.dif) <- c('x', 'y', 'dx', 'dy')

data1.dif
  x y dx dy
1 5 3 NA NA
2 8 9  3  6
3 3 1 -5 -8
4 1 5 -2  4



回答2:


Use diff with transform:

dat <- read.table(text="x   y
5   3
8   9
3   1
1   5", header=T)


transform(dat, dx=c(NA, diff(x)), dy=c(NA, diff(y)))

Yielding:

  x y dx dy
1 5 3 NA NA
2 8 9  3  6
3 3 1 -5 -8
4 1 5 -2  4

And as og dplyr:

library(dplyr)

dat %>%
    mutate(dx=c(NA, diff(x)), dy=c(NA, diff(y)))


来源:https://stackoverflow.com/questions/9405552/r-programming-how-can-i-compute-the-difference-between-two-cells-in-a-data-fram

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!