Generate a list from a vector in r - r

I have a vector like a. I would like to generate a list b from a. I only typed the first 4 components of the list. Any suggestions of quick ways to achieve this be appreciated:
a <- seq(from =0, to = 359, by = 8)
b <- list(c(0:7), c(8:(8+7)), c(16:(16+7)), c(24:(24+7)))
> b
[[1]]
[1] 0 1 2 3 4 5 6 7
[[2]]
[1] 8 9 10 11 12 13 14 15
[[3]]
[1] 16 17 18 19 20 21 22 23
[[4]]
[1] 24 25 26 27 28 29 30 31

You can create a sequence from min value of a to max and then use findInterval or cut to split the sequence based on intervals.
tmp <- seq(min(a), max(a))
split(tmp, findInterval(tmp, a))
#$`1`
#[1] 0 1 2 3 4 5 6 7
#$`2`
#[1] 8 9 10 11 12 13 14 15
#$`3`
#[1] 16 17 18 19 20 21 22 23
#$`4`
#[1] 24 25 26 27 28 29 30 31
#$`5`
#[1] 32 33 34 35 36 37 38 39
#...
Another way using Map :
Map(seq, a[-length(a)], a[-1] - 1)

This will achieve the desired result
list1 <- list()
for (i in 1:45) {
base=i*8-8
list1[[i]] <- base + 1:8
}

Related

Loss of dimensions of dataframe after applying rowMeans() in R

I subset a dataframe and i applied rowMeans() on it but the dimensions of the resultant variable ('y') are lost and i am not able to use 'y' in my further code.
dim(mtcars)
# [1] 32 11
y = rowMeans((mtcars[,3:6]))
dim(y)
# NULL
Why 'y' is no longer a dataframe?. And what can i do to get back its dimensions?.
I tried the following but it didn't work.
as.data.frame(y)
# or
data.frame(y)
When you apply rowMeans() you are creating a vector out of a dataframe. So, you are going from n rows and k columns to a nx1 vector.
For a case with n=8 and k=5 we would have:
> a=as.data.frame(matrix(1:40,8,5))
> a
V1 V2 V3 V4 V5
1 1 9 17 25 33
2 2 10 18 26 34
3 3 11 19 27 35
4 4 12 20 28 36
5 5 13 21 29 37
6 6 14 22 30 38
7 7 15 23 31 39
8 8 16 24 32 40
> rowMeans(a)
[1] 17 18 19 20 21 22 23 24

R: Creating a single vector using elements from two separate vectors [duplicate]

I have two vectors which define start (from) indices and finish (to) indices:
Start = c(1, 10, 20)
Finish = c(9, 19, 30)
I want to create a list of all Start:Finish sequences along the two vectors, i.e. generate the sequences Start[1]:Finish[1] (1:9); Start[2]:Finish[2], and so on.
## [[1]]
## [1] 1 2 3 4 5 6 7 8 9
##
## [[2]]
## [1] 10 11 12 13 14 15 16 17 18 19
##
## [[3]]
## [1] 20 21 22 23 24 25 26 27 28 29 30
Preferably in some vectorized way. The values in 'Start' vector will always be larger than the corresponding elements in 'Finish' vector.
Just use mapply:
Start = c(1,10,20)
Finish = c(9,19,30)
mapply(":", Start, Finish)
## [[1]]
## [1] 1 2 3 4 5 6 7 8 9
##
## [[2]]
## [1] 10 11 12 13 14 15 16 17 18 19
##
## [[3]]
## [1] 20 21 22 23 24 25 26 27 28 29 30
##
You could, of course, also use Vectorize, but that's just a wrapper for mapply. However, Vectorize cannot be used with primitive functions, so you'll have to specify seq.default rather than seq, or seq.int.
Example:
Vectorize(seq.default)(Start, Finish)
## [[1]]
## [1] 1 2 3 4 5 6 7 8 9
##
## [[2]]
## [1] 10 11 12 13 14 15 16 17 18 19
##
## [[3]]
## [1] 20 21 22 23 24 25 26 27 28 29 30
##
Agree with #ColonelBeauvel and #nicola, though you could use seq instead of :, hence
Start = c(1, 10, 20)
Finish = c(9, 19, 30)
Map(seq, Start, Finish)

Multiplying odd numbers in a vector

I am a beginner in R and I need to multiply odd numbers (by two) of the following vector:
x<-c(1:20)
I tried with this:
x2<-c[lapply(x,"%%",2*2)==1]
But something is wrong.
For a vector like your example comprised of consecutive integers, we can use recycling
x * c(2,1)
##[1] 2 2 6 4 10 6 14 8 18 10 22 12 26 14 30 16 34 18 38 20
More generally, we can do
x * (x%%2 + 1L)
Using base r, we can try
ifelse(x %% 2 != 0, x * 2, x)
> [1] 2 2 6 4 10 6 14 8 18 10 22 12 26 14 30 16 34 18 38 20
We could find out the indices which are odd and multiply them by 2.
inds <- as.logical(x %% 2)
x[inds] <- x[inds] * 2
x
#[1] 2 2 6 4 10 6 14 8 18 10 22 12 26 14 30 16 34 18 38 20

R Append a list wih assign

I want to define a list that will depend on the loop sequence and append this list with another list
for (i in 4:4) {
nam <- paste0("estim",i)
assign(nam, list(1:10))
assign(paste0(nam,"[2]"),list(11:40))
}
##estim4
##[[1]]
##[1] 1 2 3 4 5 6 7 8 9 10
desired output
## estim4
## [[1]]
## [1] 1 2 3 4 5 6 7 8 9 10
## [[2]]
## [1] 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
## [26] 36 37 38 39 40
any help please?
update
As mentioned by #nicola below this method is difficult to implement and granted the desired output is not exactly what I was asking for. Sorry for the imprecise question.
l <- list()
for (i in 1:1) {
l[i] <- list(list(1:10));
l[[i]][2] <- list(list(letters[1:4]))
}
## l
## [[1]]
## [[1]][[1]]
## [1] 1 2 3 4 5 6 7 8 9 10
## [[1]][[2]]
## [[1]][[2]][[1]]
## [1] "a" "b" "c" "d"
Is there a simpler way to carry variable around?

Create integer sequences defined by 'from' and 'to' vectors

I have two vectors which define start (from) indices and finish (to) indices:
Start = c(1, 10, 20)
Finish = c(9, 19, 30)
I want to create a list of all Start:Finish sequences along the two vectors, i.e. generate the sequences Start[1]:Finish[1] (1:9); Start[2]:Finish[2], and so on.
## [[1]]
## [1] 1 2 3 4 5 6 7 8 9
##
## [[2]]
## [1] 10 11 12 13 14 15 16 17 18 19
##
## [[3]]
## [1] 20 21 22 23 24 25 26 27 28 29 30
Preferably in some vectorized way. The values in 'Start' vector will always be larger than the corresponding elements in 'Finish' vector.
Just use mapply:
Start = c(1,10,20)
Finish = c(9,19,30)
mapply(":", Start, Finish)
## [[1]]
## [1] 1 2 3 4 5 6 7 8 9
##
## [[2]]
## [1] 10 11 12 13 14 15 16 17 18 19
##
## [[3]]
## [1] 20 21 22 23 24 25 26 27 28 29 30
##
You could, of course, also use Vectorize, but that's just a wrapper for mapply. However, Vectorize cannot be used with primitive functions, so you'll have to specify seq.default rather than seq, or seq.int.
Example:
Vectorize(seq.default)(Start, Finish)
## [[1]]
## [1] 1 2 3 4 5 6 7 8 9
##
## [[2]]
## [1] 10 11 12 13 14 15 16 17 18 19
##
## [[3]]
## [1] 20 21 22 23 24 25 26 27 28 29 30
##
Agree with #ColonelBeauvel and #nicola, though you could use seq instead of :, hence
Start = c(1, 10, 20)
Finish = c(9, 19, 30)
Map(seq, Start, Finish)

Resources