J'ai une liste de 900 noms comme ceux-ci:
Je suis intéressé de savoir combien de miRs ont "0" avant le dernier point de la séquence. J'ai essayé différentes combinaisons de grep et gsub (pour supprimer les lettres / chiffres après le dernier point) mais je ne peux pas le résoudre en raison de la longueur variable des lettres après à la fin. Je serai très reconnaissant de votre aide.
Le résultat attendu est soit:
3 Réponses :
Une idée via la base R,
x <- c('miR.30a.5p.11TC.0.0.0', 'miR.30a.5p.0.G.0.ag', 'miR.21.5p.0.A.0.tga', 'miR.30a.3p.0.TA.c.c')
Ou en utilisant le package stringr
,
#For the sum, sum(stringr::word(x, -2, sep = '\\.') == 0) #[1] 3 #For trimming stringr::word(x, 1, -2, sep = '\\.') #[1] "miR.30a.5p.11TC.0.0" "miR.30a.5p.0.G.0" "miR.21.5p.0.A.0" "miR.30a.3p.0.TA.c"
DONNÉES
sum(sapply(x, function(i){i1 <- strsplit(i, '.', fixed = TRUE)[[1]]; i1[(length(i1)) - 1] == 0})) #[1] 3
sum(gsub('.*\\.(.*)\\..*','\\1',x)==0) [1] 3 .* any number of characters and it may contain dot as well \\. a literal dot (.*) group of any number of characters. we will get this group back using \\1 \\..* a literal dot "the final dot" followed by any number of characters
filt <- unlist(lapply(lapply(strsplit(names, ".", fixed=T), rev), "[[", 2)) == "0" # boolean vector with TRUE where sum(filt) # nb of files with zeros as second last element
names <- c("miR.30a.5p.11TC.0.0.0", "miR.30a.5p.0.G.0.ag", "miR.21.5p.0.A.0.tga", "miR.30a.3p.0.TA.c.c", "miR.30a.5p.11TC.0.0", "miR.30a.5p.0.G.0")
Meilleur, Chris