I have the following two lists:
First list:
[[1]]
[1] "ab" "iew" "rer" "fdd"
[[2]]
[1] "ff" "de
[[3]]
[1] "cc"
Second list:
[[1]]
[1] "iew" "vfr"
[[2]]
[1] "ff" "cdc"
[[3]]
[1] "vf" "cde"
My goal is to compare these two multi-dimensional lists, so that the result would be:
[[1]]
[1] FALSE TRUE FALSE FALSE
[[2]]
[1] TRUE FALSE
[[3]]
[1] FALSE
What is the best vectorized way to preform this intersect() function?
Here's an alternative using mapply
> mapply("%in%", Firt.list, Second.list)
[[1]]
[1] FALSE TRUE FALSE FALSE
[[2]]
[1] TRUE FALSE
[[3]]
[1] FALSE
Where First.list and Second.list are:
Firt.list <- list(c("ab", "iew", "rer", "fdd" ), c("ff", "de"), c("cc"))
Second.list <- list(c("iew", "vfr"), c("ff", "cdc"), c("vf", "cde"))
If you want to know which values are the intersects of the lists, then try this
> mapply("intersect", Firt.list, Second.list)
[[1]]
[1] "iew"
[[2]]
[1] "ff"
[[3]]
character(0)
Related
I have a large list of lists with three columns (TRUE/FALSE) per case. I want to find out for which cases all three columns = TRUE.
Example data:
l1 <- list( c("TRUE","FALSE","FALSE") , c("FALSE","FALSE","FALSE") , c("FALSE","FALSE","FALSE") )
l2 <- list( c("TRUE","TRUE","TRUE") , c("TRUE","TRUE","TRUE") , c("FALSE","FALSE","FALSE") )
l3 <- list( c("TRUE","TRUE","TRUE") , c("FALSE","FALSE","FALSE") , c("TRUE","FALSE","FALSE") )
mylist <- list(l1,l2,l3)
In the output I need to see which cases in which lists meet the condition, so something like
l2[[1]]
l3[[1]]
Hope that someone can help! Thank you so much in advance!
With rapply:
rapply(mylist, \(x) all(x == "TRUE"), how = "list")
output
[[1]]
[[1]][[1]]
[1] FALSE
[[1]][[2]]
[1] FALSE
[[1]][[3]]
[1] FALSE
[[2]]
[[2]][[1]]
[1] TRUE
[[2]][[2]]
[1] TRUE
[[2]][[3]]
[1] FALSE
[[3]]
[[3]][[1]]
[1] TRUE
[[3]][[2]]
[1] FALSE
[[3]][[3]]
[1] FALSE
Or, if you want a more compact result:
rapply(mylist, \(x) all(x == "TRUE"), how = "list") |>
lapply(\(x) which(unlist(x)))
[[1]]
integer(0)
[[2]]
[1] 1 2
[[3]]
[1] 1
Another compact solution with rrapply::rrapply:
rrapply::rrapply(mylist, \(x) all(x == "TRUE"), how = "melt")
L1 L2 value
1 2 1 TRUE, TRUE, TRUE
2 2 2 TRUE, TRUE, TRUE
3 3 1 TRUE, TRUE, TRUE
Note: you probably have logical vector in your real data, which is made of c(TRUE, FALSE) (without the brackets). In that case, all(x), is sufficient.
Perhaps this helps
which(sapply(mylist, \(x) sapply(x, \(y) all(y == 'TRUE'))), arr.ind = TRUE)
or use
lapply(mylist, \(x) Filter(\(y) all(y == 'TRUE'), x))
[[1]]
list()
[[2]]
[[2]][[1]]
[1] "TRUE" "TRUE" "TRUE"
[[2]][[2]]
[1] "TRUE" "TRUE" "TRUE"
[[3]]
[[3]][[1]]
[1] "TRUE" "TRUE" "TRUE"
Or may be
> lapply(mylist, \(x) lapply(x, \(y) all(y == 'TRUE')))
[[1]]
[[1]][[1]]
[1] FALSE
[[1]][[2]]
[1] FALSE
[[1]][[3]]
[1] FALSE
[[2]]
[[2]][[1]]
[1] TRUE
[[2]][[2]]
[1] TRUE
[[2]][[3]]
[1] FALSE
[[3]]
[[3]][[1]]
[1] TRUE
[[3]][[2]]
[1] FALSE
[[3]][[3]]
[1] FALSE
Here is another trick using lapply
lapply(
mylist,
function(x) {
as.list(
colMeans(type.convert(list2DF(x), as.is = TRUE)) == 1
)
}
)
which gives
[[1]]
[[1]][[1]]
[1] FALSE
[[1]][[2]]
[1] FALSE
[[1]][[3]]
[1] FALSE
[[2]]
[[2]][[1]]
[1] TRUE
[[2]][[2]]
[1] TRUE
[[2]][[3]]
[1] FALSE
[[3]]
[[3]][[1]]
[1] TRUE
[[3]][[2]]
[1] FALSE
[[3]][[3]]
[1] FALSE
I have a list as such
$`1`
[1] TRUE
$`5`
[1] TRUE
$`14`
[1] FALSE
$`17`
[1] TRUE
$`19`
[1] TRUE
$`20`
[1] TRUE
Is there an easy way to count the total number of TRUE values in the list?
I tried doing this trucount <- function(z){sum(z,na.rm = TRUE)} , but it doesn't work.
In the above example, the solution would return 5
You can use isTRUE():
> ll = list(`1`=TRUE, `5`=TRUE, `14`=TRUE, `17`=TRUE, `19`=TRUE, `20`=TRUE, `21`=FALSE)
> length(which(sapply(ll, isTRUE)))
[1] 6
My goal is to divide a list into n groups in all possible combinations (where the group has a variable length).
I found the same question answered here (Python environment), but I'm unable to replicate it in the R environment.
Could anyone kindly help me? Thanks a lot.
If you want an easy implementation for the similar objective, you can try listParts from package partitions, e.g.,
> x <- 4
> partitions::listParts(x)
[[1]]
[1] (1,2,3,4)
[[2]]
[1] (1,2,4)(3)
[[3]]
[1] (1,2,3)(4)
[[4]]
[1] (1,3,4)(2)
[[5]]
[1] (2,3,4)(1)
[[6]]
[1] (1,4)(2,3)
[[7]]
[1] (1,2)(3,4)
[[8]]
[1] (1,3)(2,4)
[[9]]
[1] (1,4)(2)(3)
[[10]]
[1] (1,2)(3)(4)
[[11]]
[1] (1,3)(2)(4)
[[12]]
[1] (2,4)(1)(3)
[[13]]
[1] (2,3)(1)(4)
[[14]]
[1] (3,4)(1)(2)
[[15]]
[1] (1)(2)(3)(4)
where x is the number of elements in the set, and all partitions denotes the indices of elements.
If you want to choose the number of partitions, below is a user function that may help
f <- function(x, n) {
res <- listParts(x)
subset(res, lengths(res) == n)
}
such that
> f(x, 2)
[[1]]
[1] (1,2,4)(3)
[[2]]
[1] (1,2,3)(4)
[[3]]
[1] (1,3,4)(2)
[[4]]
[1] (2,3,4)(1)
[[5]]
[1] (1,4)(2,3)
[[6]]
[1] (1,2)(3,4)
[[7]]
[1] (1,3)(2,4)
> f(x, 3)
[[1]]
[1] (1,4)(2)(3)
[[2]]
[1] (1,2)(3)(4)
[[3]]
[1] (1,3)(2)(4)
[[4]]
[1] (2,4)(1)(3)
[[5]]x
[1] (2,3)(1)(4)
[[6]]
[1] (3,4)(1)(2)
Update
Given x <- LETTERS[1:4], we can run
res <- rapply(listParts(length(x)), function(v) x[v], how = "replace")
such that
> res
[[1]]
[1] (A,B,C,D)
[[2]]
[1] (A,B,D)(C)
[[3]]
[1] (A,B,C)(D)
[[4]]
[1] (A,C,D)(B)
[[5]]
[1] (B,C,D)(A)
[[6]]
[1] (A,D)(B,C)
[[7]]
[1] (A,B)(C,D)
[[8]]
[1] (A,C)(B,D)
[[9]]
[1] (A,D)(B)(C)
[[10]]
[1] (A,B)(C)(D)
[[11]]
[1] (A,C)(B)(D)
[[12]]
[1] (B,D)(A)(C)
[[13]]
[1] (B,C)(A)(D)
[[14]]
[1] (C,D)(A)(B)
[[15]]
[1] (A)(B)(C)(D)
I have the following list. As you can see the 5th element contains multiple variables. I want to split the 5th element up and insert each single variable into the overall list as an individual element.
[[1]]
[1] "319"
[[2]]
[1] "321"
[[3]]
[1] "328"
[[4]]
[1] "333"
[[5]]
[1] "344" " 345" " 346"
[[6]]
[1] "353"
I'm coding in R Studio. I want it to do this -->
[[1]]
[1] "319"
[[2]]
[1] "321"
[[3]]
[1] "328"
[[4]]
[1] "333"
[[5]]
[1] "344"
[[6]]
[1] "345"
[[7]]
[1] "346"
[[8]]
[1] "353"
I am working on text mining using tm package. I have corpus, with 320 documents in it, I would like to search for a keyword in corpus contents, such that it should return document number, So I have written like
miningCases <- lapply(myCorpusCopy,function(x){grepl(as.character(x), pattern = "\\<mining")})
Here are the first 8 results, when I print miningCases
[[1]]
[1] TRUE
[[2]]
[1] FALSE
[[3]]
[1] FALSE
[[4]]
[1] FALSE
[[5]]
[1] FALSE
[[6]]
[1] FALSE
[[7]]
[1] TRUE
[[8]]
[1] FALSE
I want to get something like 1 7, such it found pattern in 1st and 7th document. Any way to do this?