Josesec
December 31, 2022, 7:45pm
1
From the list below I would like to detect the elements of the list (5 elements in the list below) that have a string ending by "0002". The problem is in the detection of the "end" of each string because some elements of the list have more than one string.
$1
[1] 1000262
$554
[1] 554000002 554000017 554000022 554000087 554000170 554000229 554000381 554000515
[9] 554000595 554000635 554000680
$653
[1] 653000002 653000033 653000126 653000400 653000712
$654
[1] 654000579
$655
[1] 655000620
Is this a single string?
"554000002 554000017 554000022 554000087 554000170 554000229 554000381 554000515"
See the FAQ: How to do a minimal reproducible example reprex
for beginners for a way to eliminate ambiguities such as this.
FJCC
December 31, 2022, 8:00pm
3
I'm really not sure what your criterion is for the search. This gets the answer of 5 that you mentioned.
TheList <- list(`1` = 1000262,
`554` = c(554000002, 554000017, 554000022, 554000087,
554000170, 554000229, 554000381, 554000515,
554000595, 554000635, 554000680),
`653` = c(653000002, 653000033, 653000126, 653000400, 653000712),
`654` = c(654000579),
`655` = c(655000620))
TheList
#> $`1`
#> [1] 1000262
#>
#> $`554`
#> [1] 554000002 554000017 554000022 554000087 554000170 554000229 554000381
#> [8] 554000515 554000595 554000635 554000680
#>
#> $`653`
#> [1] 653000002 653000033 653000126 653000400 653000712
#>
#> $`654`
#> [1] 654000579
#>
#> $`655`
#> [1] 655000620
Counts <- sapply(TheList, function(x) sum(grepl("0002",x)))
Counts
#> 1 554 653 654 655
#> 1 3 1 0 0
sum(Counts)
#> [1] 5
Created on 2022-12-31 with reprex v2.0.2
1 Like
Josesec
December 31, 2022, 8:13pm
4
Thank you for your answer. For me this is an element of the list with 8 different strings.
Josesec
December 31, 2022, 8:16pm
5
Thank you for your answer. This is not exactly what I am looking for. I am looking for this answer:
[1] FALSE TRUE TRUE FALSE FALSE
For example: for the first element of the list there is no string ending by "0002". In the second element of the list the first string ( 5540000002) is ending by "0002".
FJCC
December 31, 2022, 8:26pm
6
TheList <- list(`1` = 1000262,
`554` = c(554000002, 554000017, 554000022, 554000087,
554000170, 554000229, 554000381, 554000515,
554000595, 554000635, 554000680),
`653` = c(653000002, 653000033, 653000126, 653000400,
653000712),
`654` = c(654000579),
`655` = c(655000620))
sapply(TheList, function(x) any(grepl("0002$",x)))
1 554 653 654 655
FALSE TRUE TRUE FALSE FALSE
1 Like
system
Closed
January 7, 2023, 8:27pm
7
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.