Remove elements in a list in R - r

I want to remove part of the list where it is a complete set of the other part of the list. For example, B intersect A and E intersect C, therefore B and E should be removed.
MyList <- list(A=c(1,2,3,4,5), B=c(3,4,5), C=c(6,7,8,9), E=c(7,8))
MyList
$A
[1] 1 2 3 4 5
$B
[1] 3 4 5
$C
[1] 6 7 8 9
$E
[1] 7 8
MyListUnique <- RemoveSubElements(MyList)
MyListUnique
$A
[1] 1 2 3 4 5
$C
[1] 6 7 8 9
Any ideas ? Any know function to do it ?

As long as your data is not too huge, you can use an approach like the following:
# preparation
MyList <- MyList[order(lengths(MyList))]
idx <- vector("list", length(MyList))
# loop through list and compare with other (longer) list elements
for(i in seq_along(MyList)) {
idx[[i]] <- any(sapply(MyList[-seq_len(i)], function(x) all(MyList[[i]] %in% x)))
}
# subset the list
MyList[!unlist(idx)]
#$C
#[1] 6 7 8 9
#
#$A
#[1] 1 2 3 4 5

Similar to the other answer, but hopefully clearer, using a helper function and 2 sapplys.
#helper function to determine a proper subset - shortcuts to avoid setdiff calculation if they are equal
is.proper.subset <- function(x,y) !setequal(x,y) && length(setdiff(x,y))==0
#double loop over the list to find elements which are proper subsets of other elements
idx <- sapply(MyList, function(x) any(sapply(MyList, function(y) is.proper.subset(x,y))))
#filter out those that are proper subsets
MyList[!idx]
$A
[1] 1 2 3 4 5
$C
[1] 6 7 8 9

Related

how to make lists of lists into one consecutive list r

Here is my list of lists
$cor
[1] 0.10194656 0.09795653 0.18832356 0.12265421 0.09669338 0.13781369 0.16763787 0.15137726 0.10203826 0.12649443 0.16451622 0.18429656 0.21234920
[14] 0.18254895 0.10761731 0.15354220 0.13458206
$cor
[1] 0.3332299 0.3909873 0.3631544
$cor
[1] 0.11601617 0.10834637 0.10138418 0.13864724 0.17582967 0.15005935 0.05481153 0.15443826 0.08987235 0.19109966 0.13404778 0.15816381
I want all values to be in one list in the order they appear.
I'm not sure what you are trying to do. But base R has an unlist function which outputs what you may be looking for:
c1 <- 1:5
c2 <- 6:10
cor <- list(c1, c2)
cor
[[1]]
[1] 1 2 3 4 5
[[2]]
[1] 6 7 8 9 10
unlist(cor)
[1] 1 2 3 4 5 6 7 8 9 10
Using the following example data:
lst <- list(cor = 1:3, cor = 4:6)
lst
$cor
[1] 1 2 3
$cor
[1] 4 5 6
You could use the following, which places all items in one vector, in their original order:
do.call(c, lst)
cor1 cor2 cor3 cor1 cor2 cor3
1 2 3 4 5 6
Reduce() also works similarly:
Reduce(c, lst)
[1] 1 2 3 4 5 6

How to organize the output of the list of list in R

Suppose this is my list of list (I would like to organize the result as my data contains more than 40 results and it is difficult for me to organize them manually).
s <- c(1,2,3)
ss <- c(4,5,6)
S <- list(s,ss)
h <- c(4,8,7)
hh <- c(0,3,4)
H <- list(h,hh)
HH <- list(S,H)
names1 <- c("First","Second")
lapply(setNames(HH, paste0(names1, '_Model')), function(x)
setNames(x, paste0('Res_', seq_along(x))))
#$First_Model
#$First_Model$Res_1
#[1] 1 2 3
#$First_Model$Res_2
#[1] 4 5 6
#$Second_Model
#$Second_Model$Res_1
#[1] 4 8 7
#$Second_Model$Res_2
#[1] 0 3 4
I would like to have the result similar to the following:
#$First_Model
#$First_Model$Res_1
#[1] 1 2 3
#$Second_Model
#$Second_Model$Res_1
#[1] 4 8 7
#$First_Model$Res_2
#[1] 4 5 6
#$Second_Model$Res_2
#[1] 0 3 4
The problem in question is how to rearrange the nested list from "Model No. > Results No." to "Results No. > Model No."
I was going for something similar to Wimpel's answer.
Res_no <- seq_along(HH[[1]]) # results elements
lapply(setNames(Res_no, paste0("Res_", Res_no)), function(x)
lapply(setNames(HH, paste0(names1, '_Model')), `[[`, x)
)
Output
#$Res_1
#$Res_1$First_Model
#[1] 1 2 3
#
#$Res_1$Second_Model
#[1] 4 8 7
#
#
#$Res_2
#$Res_2$First_Model
#[1] 4 5 6
#
#$Res_2$Second_Model
#[1] 0 3 4
The base of this solution is to extract the x-th element of the nested list (seen in the inner lapply() function of the code). You can do this with lapply or purrr:map, as described here.
The outer lapply() function lets you repeat it for all the "Results No."
Something like this perhaps?
# From your code, create a list L
L <- lapply(setNames(HH, paste0(names1, '_Model')), function(x)
setNames(x, paste0('Res_', seq_along(x))))
# get all x-th elements from the list, and add them to new list L2
L2 <- lapply( 1:length(L[[1]]), function(x) {
lapply(L, "[[", x)
})
# set names of L2
names(L2) <- names(L[[1]])
output
# $Res_1
# $Res_1$First_Model
# [1] 1 2 3
#
# $Res_1$Second_Model
# [1] 4 8 7
#
#
# $Res_2
# $Res_2$First_Model
# [1] 4 5 6
#
# $Res_2$Second_Model
# [1] 0 3 4

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

remove certain vectors from a list

I want to remove certain vectors from a list. I have for example this:
a<-c(1,2,5)
b<-c(1,1,1)
c<-c(1,2,3,4)
d<-c(1,2,3,4,5)
exampleList<-list(a,b,c,d)
exampleList returns of course:
[[1]]
[1] 1 2 5
[[2]]
[1] 1 1 1
[[3]]
[1] 1 2 3 4
[[4]]
[1] 1 2 3 4 5
Is there a way to remove certain vectors from a list in R. I want to remove all vectors in the list exampleList which contain both 1 and 5(so not only vectors which contain 1 or 5, but both). Thanks in advance!
Use Filter:
filteredList <- Filter(function(v) !(1 %in% v & 5 %in% v), exampleList)
print(filteredList)
#> [[1]]
#> [1] 1 1 1
#>
#> [[2]]
#> [1] 1 2 3 4
Filter uses a functional style. The first argument you pass is a function that returns TRUE for an element you want to keep in the list, and FALSE for an element you want to remove from the list. The second argument is just the list itself.
We can use sapply on every list element and remove those elements where both the values 1 and 5 are present.
exampleList[!sapply(exampleList, function(x) any(x == 1) & any(x == 5))]
#[[1]]
#[1] 1 1 1
#[[2]]
#[1] 1 2 3 4
Here a solution with two steps:
exampleList<-list(a=c(1,2,5), b=c(1,1,1), c=c(1,2,3,4), d=c(1,2,3,4,5))
L <- lapply(exampleList, function(x) if (!all(c(1,5) %in% x)) x)
L[!sapply(L, is.null)]
# $b
# [1] 1 1 1
#
# $c
# [1] 1 2 3 4
Here is a one-step variant without any definition of a new function
exampleList[!apply(sapply(exampleList, '%in%', x=c(1,5)), 2, all)]
(... but it has two calls to apply-functions)

Getting index of vector with delimited parts

I have vectors that looks like these variations:
cn1 <- c("Probe","Genes","foo","bar","Probe","Genes","foo","bar")
# 0 1 2 3 4 5 6 7
cn2 <- c("Probe","Genes","foo","bar","qux","Probe","Genes","foo","bar","qux")
# 0 1 2 3 4 5 6 7 8 9
Note that in each vector above consists of two parts. They are separated with "Probe","Genes".
What I want to do is to get the indexes of the first part of the entry in between that separator. Yielding
cn1_id ------> [2,3]
cn2_id ------> [2,3,4]
How can I achieve that in R?
I tried this but it doesn't do what I want:
> split(cn1,c("Probe","Genes"))
$Genes
[1] "Genes" "bar" "Genes" "bar"
$Probe
[1] "Probe" "foo" "Probe" "foo"
Here's a function that you can use. Note that R vectors are 1-based so counting starts at 1 rather than 0.
findidx <- function(x) {
idx <- which(x=="Probe" & c(tail(x,-1),NA)=="Genes")
if (length(idx)>1) {
(idx[1]+2):(idx[2]-1)
} else {
NA # what to return if no match found
}
}
findidx(cn1)
# [1] 3 4
findidx(cn2)
# [1] 3 4 5
You could try between from data.table
indx <- between(cn1, 'Genes', 'Probe')
indx2 <- between(cn2, 'Genes', 'Probe')
which(cumsum(indx)==2)[-1]-1
#[1] 2 3
which(cumsum(indx2)==2)[-1]-1
#[1] 2 3 4

Resources