Can I subset one vector in subvectors according to number of elements?

Advertisements

I want subset my vector of length 30 in 5 subvectors according to number of elements provided by me.
The subvector1 must contain 17 elements, subvector2 5 elements, subvector3 1 element, subvector 4 5 elements, subvector 5 2 elements.
I don’t want to change the order, meaning that subvector1 will contain the first 17 elements of the original vector, subvector2 will contain 18th:22nd elemnts f the original vector and so on…

data_controllo<- c("2018-09-24 UTC","2018-10-22 UTC", "2018-11-21 UTC", "2018-12-19 UTC", "2019-01-30 UTC", "2019-03-01 UTC", "2019-03-27 UTC", "2019-04-29 UTC","2019-05-31 UTC", "2019-07-30 UTC", "2019-08-28 UTC", "2019-09-26 UTC", "2019-10-22 UTC","2019-11-20 UTC","2019-12-16 UTC", "2020-01-21 UTC", "2020-02-17 UTC", "2020-04-21 UTC", "2020-05-19 UTC", "2020-06-16 UTC", "2020-07-14 UTC", "2020-08-18 UTC", "2020-09-16 UTC", "2020-10-13 UTC", "2020-12-09 UTC", "2021-01-12 UTC", "2021-02-12 UTC", "2021-03-11 UTC", "2021-04-09 UTC", "2021-05-10 UTC")

subset(data_controllo, ...) # i cannot continue
#probably i have to create a vector containing the number of elements i would have for the new vectors?! like

elements_perVector<-c(17, 5, 1, 5,2)
# THIS IS WHAT I EXPECT
> subvector1
 [1] "2018-09-24 UTC" "2018-10-22 UTC" "2018-11-21 UTC" "2018-12-19 UTC" "2019-01-30 UTC" "2019-03-01 UTC" "2019-03-27 UTC" "2019-04-29 UTC"
 [9] "2019-05-31 UTC" "2019-07-30 UTC" "2019-08-28 UTC" "2019-09-26 UTC" "2019-10-22 UTC" "2019-11-20 UTC" "2019-12-16 UTC" "2020-01-21 UTC"
[17] "2020-02-17 UTC"
> subvector2
 [1] "2020-04-21 UTC" "2020-05-19 UTC" "2020-06-16 UTC" "2020-07-14 UTC" "2020-08-18 UTC"
> subvector3
 [1] "2020-09-16 UTC"
> subvector4
 [1] "2020-10-13 UTC" "2020-12-09 UTC" "2021-01-12 UTC" "2021-02-12 UTC" "2021-03-11 UTC"
> subvector5
 [1] "2021-04-09 UTC" "2021-05-10 UTC"

>Solution :

We can use split.

lst <- split(data_controllo, rep(paste0("subvector", 1:length(elements_perVector)), elements_perVector))

$subvector1
 [1] "2018-09-24 UTC" "2018-10-22 UTC" "2018-11-21 UTC" "2018-12-19 UTC"
 [5] "2019-01-30 UTC" "2019-03-01 UTC" "2019-03-27 UTC" "2019-04-29 UTC"
 [9] "2019-05-31 UTC" "2019-07-30 UTC" "2019-08-28 UTC" "2019-09-26 UTC"
[13] "2019-10-22 UTC" "2019-11-20 UTC" "2019-12-16 UTC" "2020-01-21 UTC"
[17] "2020-02-17 UTC"

$subvector2
[1] "2020-04-21 UTC" "2020-05-19 UTC" "2020-06-16 UTC" "2020-07-14 UTC"
[5] "2020-08-18 UTC"

$subvector3
[1] "2020-09-16 UTC"

$subvector4
[1] "2020-10-13 UTC" "2020-12-09 UTC" "2021-01-12 UTC" "2021-02-12 UTC"
[5] "2021-03-11 UTC"

$subvector5
[1] "2021-04-09 UTC" "2021-05-10 UTC"

The result lst is a list. To get 5 separate vectors, use list2env.

list2env(lst, envir = .GlobalEnv)

Leave a ReplyCancel reply