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?
Related
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
}
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)
I have the following question. I have a list, which holds a list of vectors list like these. Mylist:
[[1]]
[[1]][[1]]
[1] 1 2 3 4 5 6 7 8 9 10
[[1]][[2]]
[1] 6 7 8 9 10 11 12 13 14 15
[[2]]
[[2]][[1]]
[1] 11 12 13 14 15 16 17 18 19 20
[[2]][[2]]
[1] 16 17 18 19 20 21 22 23 24 25
[[3]]
[[3]][[1]]
[1] 3 4 5 6 7 8 9 10 11 12
[[3]][[2]]
[1] 6 7 8 9 10 11 12 13 14 15
....
Each sublist within the list holds only two elements (vectors) and I would like to find a solution to multiply the two vectors stored inside each sublist of the list. In other words Mylist[[1]][[1]]*Mylist[[1]][[2]], Mylist[[2]][[1]]*Mylist[[2]][[2]], Mylist[[3]][[1]]*Mylist[[3]][[2]], and so on... I could easily do a for loop but I know it takes too much time and I would like to know if there is away I can use an apply function or something similar.
If L is your list try:
lapply(L, do.call, what = "*")
The apply functions are very similar to for loops. Using the lapply function:
newlist <- lapply(Mylist, function(x) x[[1]] * x[[2]])
You could also use lapply and Reduce:
lapply(myList, Reduce, f="*")
[[1]]
[1] 6 14 24 36 50 66 84 104 126 150
[[2]]
[1] 176 204 234 266 300 336 374 414 456 500
data, first two list elements in OP's example
myList <- list(list(1:10, 6:15), list(11:20, 16:25))
I have a matrix (RR) that the column names are integer. When I refer to the elements of the marix like RR[x, c("5")] it works fine but when I put change it to
Myindex <-5
RR[x, c("Myindex")]
I get the error subscript out of bounds. I could not understand it so far.
BTW, 5 is just an example.
Any idea?
Thanks
Even though you name the column names as numbers it is taken as character column names.
rr <- matrix(1:15,3,5)
colnames(rr) <- c(21:25)
rr
# 21 22 23 24 25
# [1,] 1 4 7 10 13
# [2,] 2 5 8 11 14
# [3,] 3 6 9 12 15
rr[1,"23"]
# 23 ## column name is 23
# 7
my_index <- 4
rr[3,my_index]
# 24 ## column name is 24
# 12
my_index <- "25"
rr[3,my_index]
# 25 ## column name is 25
# 15
colnames(rr) <- as.integer(c(21:25))
rr
# 21 22 23 24 25
# [1,] 1 4 7 10 13
# [2,] 2 5 8 11 14
# [3,] 3 6 9 12 15
class(colnames(rr))
# [1] "character"
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)