简体   繁体   中英

R: subtracting the first row from itself and subsequent rows from next row in column & do this for multiple columns

I have many groups in my DF with timeseries data that does not begin at 0. I would like to normalize my data by setting the values in the first row of every group to 0 such that the FrameTime and associated values are all set to 0. To do this I need to subtract the first row value from itself and then subtract the following row's value from the next row, so on and so forth. I would like a function that allows me to do this over many columns such as mutate_at().

I have tried

tmp2 <- tmp %>%
  group_by(Name,StimulusName) %>%
  mutate_at(8:37, funs(c(first(.), (. - first(.))[-1])) )

but this subtracts the first row value from all other rows and not exactly what I need.

You want to use dplyr's lag function to get the value of the previous row. Within the lag function you want to set the default to the first value using dplyr's first function. I created an example below:

library(dplyr)
example.data <-
  data.frame(a = c(1, 3, 6, 2, 4 ,7),
             b = c("red", "red", "red", "blue", "blue", "blue"))

example.data %>%
  group_by(b) %>%
  mutate(c = a - lag(a, n = 1, default = first(a)))

I am assuming that you have a data frame with column A containing values n1,n2,n3,...,nn and you want to mutate a new column ( B ) with values n(n) - n(n-1) from column A . The first value will be a zero, value 2 will be n2 - n1, value 3 will be n3 - n2 and so on. You can achieve this using the following in R.

library(dplyr)

data %>%
    mutate(B = A - first(x = A))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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