Generate collection of pairs/triplets in R - r

I'm generating pairs of objects from a vector, and there may be a triplet if the vector is of odd length. How can I store these and keep them as pairs/triplets? The pairs are vectors. I've tried adding to a vector like
vect <- c(vect, pair)
but it flattens all the pairs in the vector. I also tried appending to a list with list <- list(list, pair) and list <- list(list, list(pair)), as well as list.append() from rlist but none of that seems to work well.

You can do this with split you just need to give it a vector that says how you want to split the array.
ARRAY = 1:13
SF = rep(1:(length(ARRAY)/2), each=2)
if(length(SF) < length(ARRAY)) { SF = c(SF, SF[length(SF)]) }
split(ARRAY, SF)
$`1`
[1] 1 2
$`2`
[1] 3 4
$`3`
[1] 5 6
$`4`
[1] 7 8
$`5`
[1] 9 10
$`6`
[1] 11 12 13

Related

What is the best way to append values to a sublist of a list in R?

Say I have a list l containing sublists, and I would like to add an element (or several elements) at the end of each sublist of that list.
What is the best way to implement this (simplest/fastest code)?
# Goal: Append given elements to each sublist of a list in R
# Example: add c(9) to sublists of list l
l <- list(c(1,2,3), c(2,1,4), c(4,7,6))
l
[[1]]
[1] 1 2 3
[[2]]
[1] 2 1 4
[[3]]
[1] 4 7 6
# Desired Output:
# l_9
# [[1]]
# [1] 1 2 3 9
# [[2]]
# [1] 2 1 4 9
# [[3]]
# [1] 4 7 6 9
In these cases is useful to use lapply function. You can see here: apply functions
So you can just use l <- lapply(l, c, 9)
where c is the function of combining. Here: c function

Replace empty element in a list using sapply

I have two lists. The first one has an empty element. I'd like to replace that empty element with the first vector of the third list element of another list.
l1 <- list(a=1:3,b=4:9,c="")
l2 <- list(aa=11:13,bb=14:19,cc=data.frame(matrix(100:103,ncol=2)))
l1[sapply(l1, `[[`, 1)==""] <- l2[[3]][[1]]
Using sapply, I can identify which elements are empty. However, when I try to assign a vector to this empty element: I get this error message:
Warning message: In l1[sapply(l1, [[, 1) == ""] <- l2[[3]][[1]] :
number of items to replace is not a multiple of replacement length
This is only a warning, but the result I get is not the one I want. This is the l1 I get:
> l1
$a
[1] 1 2 3
$b
[1] 4 5 6 7 8 9
$c
[1] 100
This is what I need (two elements in $c):
> l1
$a
[1] 1 2 3
$b
[1] 4 5 6 7 8 9
$c
[1] 100 101
Just use l2[[3]][1] on the right hand side (single [ not [[)
The right-hand side should be a list, since you're replacing a list element. So you want that to be
... <- list(l2[[3]][[1]])
In addition, you might consider using !nzchar(l1) in place of sapply(...) == "". It might be more efficient. The final expression would be:
l1[!nzchar(l1)] <- list(l2[[3]][[1]])
giving the updated l1:
$a
[1] 1 2 3
$b
[1] 4 5 6 7 8 9
$c
[1] 100 101

R: how to find index of all repetition vector values order by unique vector without using loop?

I have a vector of integers like this:
a <- c(2,3,4,1,2,1,3,5,6,3,2)
values<-c(1,2,3,4,5,6)
I want to list, for every unique value in my vector (the unique values being ordered), the position of their occurences. My desired output:
rep_indx<-data.frame(c(4,6),c(1,5,11),c(2,7,10),c(3),c(8),c(9))
split fits pretty well here, which returns a list of indexes for each unique value in a:
indList <- split(seq_along(a), a)
indList
# $`1`
# [1] 4 6
#
# $`2`
# [1] 1 5 11
#
# $`3`
# [1] 2 7 10
#
# $`4`
# [1] 3
#
# $`5`
# [1] 8
#
# $`6`
# [1] 9
And you can access the index by passing the value as a character, i.e.:
indList[["1"]]
# [1] 4 6
You can do this, using sapply. The ordering that you need is ensured by the sort function.
sapply(sort(unique(a)), function(x) which(a %in% x))
#### [[1]]
#### [1] 4 6
####
#### [[2]]
#### [1] 1 5 11
#### ...
It will result in a list, giving the indices of your repetitions. It can't be a data.frame because a data.frame needs to have columns of same lengths.
sort(unique(a)) is exactly your vector variable.
NOTE: you can also use lapply to force the output to be a list. With sapply, you get a list except if by chance the number of replicates is always the same, then the output will be a matrix... so, your choice!
Perhaps this also works
order(match(a, values))
#[1] 4 6 1 5 11 2 7 10 3 8 9
You can use the lapply function to return a list with the indexes.
lapply(values, function (x) which(a == x))

Get all diagonal vectors from matrix

I'm trying to figure out how to get all the diagonals of a matrix.
For example, say I have the following matrix:
A <- matrix(1:16,4)
using the diag(A) function will return
[1] 1 6 11 16
In addition to the primary diagonal, I would like a list of all the diagonals above and below it.
5 10 15
2 7 12
9 14
3 8
4
13
I found the following link https://stackoverflow.com/a/13049722 which gives me the diagonals directly above and below the primary one, however I cannot seem to figure out how to extend the code to get the rest of them for any size matrix. I tried two nested for loops since it appears that some kind of incrementing of the matrix subscripts would produce the result I am looking for. I tried using ncol(A), nrow(A) in the for loops, but couldn't seem to figure out the right combination. Plus I am aware that for loops are generally frowned upon in R.
The code given was:
diag(A[-4,-1])
diag(A[-1,-4])
which returned the two diagonals, both upper and lower
Of course this is a square matrix and not all of the matrices I want to perform this on will be square. Filling in the non-square area with NAs would be acceptable if necessary. The answer I need may be in one of the other answers on the page, but the original question involved means, sums, etc. which added a layer of complexity beyond what I am trying to do. I have a feeling the solution to this will be ridiculously simple, but it just isn't occurring to me. I'm also surprised I was not able to find this question anywhere on SO, it would seem to be a common enough question. Maybe I don't know the proper terminology for this problem.
A <- matrix(1:16, 4)
# create an indicator for all diagonals in the matrix
d <- row(A) - col(A)
# use split to group on these values
split(A, d)
#
# $`-3`
# [1] 13
#
# $`-2`
# [1] 9 14
#
# $`-1`
# [1] 5 10 15
#
# $`0`
# [1] 1 6 11 16
#
# $`1`
# [1] 2 7 12
#
# $`2`
# [1] 3 8
#
# $`3`
# [1] 4
Since you're dealing with square matrices, it should be really easy to convert Gavin's answer into a small function that first calculates the range that should be used as the offset values. Here's such a function:
AllDiags <- function(inmat, sorted = TRUE) {
Range <- ncol(inmat) - 1
Range <- -Range:Range
if (isTRUE(sorted)) Range <- Range[order(abs(Range))]
lapply(Range, function(x) {
inmat[row(inmat) == (col(inmat) - x)]
})
}
Here's the output on your sample matrix "A".
AllDiags(A)
# [[1]]
# [1] 1 6 11 16
#
# [[2]]
# [1] 2 7 12
#
# [[3]]
# [1] 5 10 15
#
# [[4]]
# [1] 3 8
#
# [[5]]
# [1] 9 14
#
# [[6]]
# [1] 4
#
# [[7]]
# [1] 13
Here is one solution based on an observation that you can get all the diagonals by shrinking and expanding the matrix. That is first consider row N col 1 (get diag of that) then rows (N-1): and cols (1:2). Get diagonal of that. etc..
N <- ncol(A)
rows <- cbind(c(N:1, rep(1,N-1)), c(rep(N,N), (N-1):1)) # row indeces
cols <- apply(rows, 2, rev) # col indeces
diagMatSubset <- function(mat, i1, i2, j1, j2) diag(mat[i1:i2, j1:j2, drop=FALSE])
Map(diagMatSubset, list(A), rows[,1], rows[,2], cols[,1], cols[,2])
[[1]]
[1] 4
[[2]]
[1] 3 8
[[3]]
[1] 2 7 12
[[4]]
[1] 1 6 11 16
[[5]]
[1] 5 10 15
[[6]]
[1] 9 14
[[7]]
[1] 13

Naming list items via loop in R

I want to take a list, create a String vector of the names of the list items, filling in blanks with a generic name and then set the names vector as the names of the list.
My code works fine for list who dont have items with names in it. It however does nothing, when there are items with a name in it.
addNamesToList <- function(myList){
listNames <- vector()
for(i in 1:length(myList)){
if(identical(names(myList[i]),NULL)){
listNames <- c(listNames,paste("item",i,sep=""))
}else{
listNames <- c(listNames,names(myList[i]))
}
}
names(myList) <- listNames
return (myList)
}
result without named items
$item1
[1] 2 3 4
$item2
[1] "hey" "ho"
result with named items
[[1]]
[1] 2 3 4
[[2]]
[1] "hey" "ho"
$hello
[1] 2 3 4
Hope you can help.
It sounds like you want to insert names where there is not currently a name. If that's the case, I would suggest using direct assignment via names(x) <- value, instead of using a loop to fill in the blanks.
In the following example, lst creates a sample list of three elements, the second of which is not named. Notice that even if only one of the list element has a name, its names vector is a character vector the same length as lst.
( lst <- list(item1 = 1:5, 6:10, item3 = 11:15) )
# $item1
# [1] 1 2 3 4 5
#
# [[2]]
# [1] 6 7 8 9 10
#
# $item3
# [1] 11 12 13 14 15
names(lst)
# [1] "item1" "" "item3"
We can insert a name into the empty name element with the following. This will also work with a vector, provided the right side vector is the same length as the left side vector.
names(lst)[2] <- "item2"
lst
# $item1
# [1] 1 2 3 4 5
#
# $item2
# [1] 6 7 8 9 10
#
# $item3
# [1] 11 12 13 14 15
For a longer list containing sporadic empty names, you can use
names(list)[!nzchar(names(list))] <- namesToAdd
nzchar basically means "non-zero character" and returns a logical, TRUE if the element is a non-zero length string.

Resources