Get a sublist of a list

Viewed 13882

I have a vector which is around 3000 elements long. I have extracted a specific point in the vector with which(...). Now I want to have -120 before this point and +120 after this point.

My list looks like that:

> testList$Date

     [1] "01.01.2002" "02.01.2002" "03.01.2002" "04.01.2002" "07.01.2002"
       [6] "08.01.2002" "09.01.2002" "10.01.2002" "11.01.2002" "14.01.2002"
      [11] "15.01.2002" "16.01.2002" "17.01.2002" "18.01.2002" "21.01.2002"
      [16] "22.01.2002" "23.01.2002" "24.01.2002" "25.01.2002" "28.01.2002"
      [21] "29.01.2002" "30.01.2002" "31.01.2002" "01.02.2002" "04.02.2002"
      [26] "05.02.2002" "06.02.2002" "07.02.2002" "08.02.2002" "11.02.2002"
      [31] "12.02.2002" "13.02.2002" "14.02.2002" "15.02.2002" "18.02.2002"ect....

I could do a for-loop to iterate over the list and save this as a sublist. However, I do not think that is very efficient. How can I implement that in R?

I appreciate your answer!

UPDATE When using lapply I get:

> 120BeforeSublist <- lapply(event, function(x) c(x-120, x))
> (120BeforeSublist)
[[1]]
[1] 1875 1995

However I want to have the sublist saved -120 before and +120 after.

2 Answers
Related