0

I'm struggling a lot right now and it would be great if someone could help me to fix the problem for my master thesis.

I have a huge data frame because heart rates were measured by seconds. The details:

  • I have 4 timepoints (timepoint 0, timepoint 1, timepoint 2 and timepoint 3).
  • Timepoint 1 to 3 consist of 661 rows each (661 seconds per timepoint).
  • Timepoint 0 only refers to one row, but the problem is that it starts with second 1 instead of second 0.

What I want is, that timepoint 0 shows second 0 and timepoint 1 shows second 1 and so on. For better understanding, here are two tables showing the old version and the desired version I'd like to have (not in excel but in r studio):

old version, this is how it looks like right now

desired version, this is what I'd like to have

Atm I'm only able to replace the TS_0 = 1 with TS_0 = 0 for timepoint 0 but I cannot change every row manually afterwards by using df$TS_0[df$TS_0 == 1] <- 0, df$TS_0[df$TS_0 == 2] <- 1 and so on...

My question is, is there anyway to change TS_0 = 1 to 0 with timepoint = 0 so that all the following rows change automatically?

I appreciate any help! Thank you :)

5
  • Please do not post (only) an image of code/data/errors: it breaks screen-readers and it cannot be copied or searched (ref: meta.stackoverflow.com/a/285557 and xkcd.com/2116). Please include the code, console output, or data (e.g., data.frame(...) or the output from dput(head(x))) directly.
    – r2evans
    May 18, 2022 at 13:40
  • See stackoverflow.com/q/5963269 for discussion on providing representative data in a usable form (e.g., dput(.), data.frame(.), read.table, or even the raw contents of a CSV file).
    – r2evans
    May 18, 2022 at 13:41
  • Since it looks like you want to decrement all values of TS_0, would df$TS_0 <- df$TS_0 - 1L work?
    – r2evans
    May 18, 2022 at 13:42
  • ... I have to wonder, though, if your data should be grouped somehow ... that is, if you have another timepoint=0 row, do you want TS_0 to reset to 0 for that row and then cumulative increment from there?
    – r2evans
    May 18, 2022 at 13:43
  • 1
    @r2evans thank you a lot, df$TS_0 <- df$TS_0 - 1Lsolved it! Thank you so much :)
    – Isabell
    May 18, 2022 at 15:16

1 Answer 1

0

Not sure what the format is of TS_0 here I demo with a simple 1 column data.table, where it are strings.

dt <- data.table(
  TS_0 = c("1 second", paste(3:10, "seconds"))
)

dt[, TS_0 := duration(TS_0) - 1][, timepoint := as.numeric(TS_0) - shift(as.numeric(TS_0), fill = 0L, type = "lag")]

dt

#    TS_0 timepoint
# 1:   0s         0
# 2:   2s         2
# 3:   3s         1
# 4:   4s         1
# 5:   5s         1
# 6:   6s         1
# 7:   7s         1
# 8:   8s         1
# 9:   9s         1
  1. When you make it a duration, you can easily substract 1 to start with 0.
  2. timepoint you calculate using the lag (note I skipped the 1s row in my example so we get 2 there instead of 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.