mauede at alice.it
2010-May-27 14:37 UTC
[R] how to extract the 1st field from a vector of strings
I have the following vector of strings (shown only the first 3 elements)> desc[1:3][1] "hsa-let-7a MIMAT0000062 Homo sapiens let-7a" [2] "hsa-let-7a* MIMAT0004481 Homo sapiens let-7a*" [3] "hsa-let-7a-2* MIMAT0010195 Homo sapiens let-7a-2*"> is.vector(desc)[1] TRUE> A <- unlist(strsplit(desc[1:3], " ")) > A[1] "hsa-let-7a MIMAT0000062 Homo sapiens let-7a" [2] "hsa-let-7a* MIMAT0004481 Homo sapiens let-7a*" [3] "hsa-let-7a-2* MIMAT0010195 Homo sapiens let-7a-2*"> as.vector(A)[1] "hsa-let-7a MIMAT0000062 Homo sapiens let-7a" [2] "hsa-let-7a* MIMAT0004481 Homo sapiens let-7a*" [3] "hsa-let-7a-2* MIMAT0010195 Homo sapiens let-7a-2*">I would like to extract only the first field (of variable length). That is I need a vector containing "hsa-let-7a " "hsa-let-7a*" "hsa-let-7a-2*" The operator [[]][] works only on the single vector element. I would like to extract the 1st field with one single instruction rather than a loop as traditional programming languages request. Thank you in advance for you help. Maura tutti i telefonini TIM! [[alternative HTML version deleted]]
Henrique Dallazuanna
2010-May-27 14:58 UTC
[R] how to extract the 1st field from a vector of strings
Try this: gsub(" MIMA.*", "\\1", desc) On Thu, May 27, 2010 at 11:37 AM, <mauede@alice.it> wrote:> I have the following vector of strings (shown only the first 3 elements) > > > desc[1:3] > [1] "hsa-let-7a MIMAT0000062 Homo sapiens let-7a" > [2] "hsa-let-7a* MIMAT0004481 Homo sapiens let-7a*" > [3] "hsa-let-7a-2* MIMAT0010195 Homo sapiens let-7a-2*" > > is.vector(desc) > [1] TRUE > > A <- unlist(strsplit(desc[1:3], " ")) > > A > [1] "hsa-let-7a MIMAT0000062 Homo sapiens let-7a" > [2] "hsa-let-7a* MIMAT0004481 Homo sapiens let-7a*" > [3] "hsa-let-7a-2* MIMAT0010195 Homo sapiens let-7a-2*" > > as.vector(A) > [1] "hsa-let-7a MIMAT0000062 Homo sapiens let-7a" > [2] "hsa-let-7a* MIMAT0004481 Homo sapiens let-7a*" > [3] "hsa-let-7a-2* MIMAT0010195 Homo sapiens let-7a-2*" > > > I would like to extract only the first field (of variable length). That is > I need a vector containing > "hsa-let-7a " > "hsa-let-7a*" > "hsa-let-7a-2*" > > The operator [[]][] works only on the single vector element. I would like > to extract the 1st field > with one single instruction rather than a loop as traditional programming > languages request. > > Thank you in advance for you help. > Maura > > > > tutti i telefonini TIM! > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
mauede at alice.it
2010-May-27 15:18 UTC
[R] PROBLEM SOLVED: how to extract the 1st field from a vector of strings
-----Messaggio originale----- Da: mauede@alice.it Inviato: gio 27/05/2010 16.37 A: r-help@stat.math.ethz.ch Oggetto: how to extract the 1st field from a vector of strings I have the following vector of strings (shown only the first 3 elements)> desc[1:3][1] "hsa-let-7a MIMAT0000062 Homo sapiens let-7a" [2] "hsa-let-7a* MIMAT0004481 Homo sapiens let-7a*" [3] "hsa-let-7a-2* MIMAT0010195 Homo sapiens let-7a-2*"> is.vector(desc)[1] TRUE> A <- unlist(strsplit(desc[1:3], " ")) > A[1] "hsa-let-7a MIMAT0000062 Homo sapiens let-7a" [2] "hsa-let-7a* MIMAT0004481 Homo sapiens let-7a*" [3] "hsa-let-7a-2* MIMAT0010195 Homo sapiens let-7a-2*"> as.vector(A)[1] "hsa-let-7a MIMAT0000062 Homo sapiens let-7a" [2] "hsa-let-7a* MIMAT0004481 Homo sapiens let-7a*" [3] "hsa-let-7a-2* MIMAT0010195 Homo sapiens let-7a-2*">I would like to extract only the first field (of variable length). That is I need a vector containing "hsa-let-7a " "hsa-let-7a*" "hsa-let-7a-2*" The operator [[]][] works only on the single vector element. I would like to extract the 1st field with one single instruction rather than a loop as traditional programming languages request. Thank you in advance for you help. Maura tutti i telefonini TIM! tutti i telefonini TIM! [[alternative HTML version deleted]]
Gabor Grothendieck
2010-May-31 17:43 UTC
[R] how to extract the 1st field from a vector of strings
Try replacing a space followed by anything (.*) with the empty string:> x <- c("hsa-let-7a MIMAT0000062 Homo sapiens let-7a",+ "hsa-let-7a* MIMAT0004481 Homo sapiens let-7a*", + "hsa-let-7a-2* MIMAT0010195 Homo sapiens let-7a-2*")> > sub(" .*", "", x)[1] "hsa-let-7a" "hsa-let-7a*" "hsa-let-7a-2*" On Thu, May 27, 2010 at 10:37 AM, <mauede at alice.it> wrote:> I have the following vector of strings (shown only the first 3 elements) > >> desc[1:3] > [1] "hsa-let-7a MIMAT0000062 Homo sapiens let-7a" > [2] "hsa-let-7a* MIMAT0004481 Homo sapiens let-7a*" > [3] "hsa-let-7a-2* MIMAT0010195 Homo sapiens let-7a-2*" >> is.vector(desc) > [1] TRUE >> A <- unlist(strsplit(desc[1:3], " ?")) >> A > [1] "hsa-let-7a ?MIMAT0000062 Homo sapiens let-7a" > [2] "hsa-let-7a* ?MIMAT0004481 Homo sapiens let-7a*" > [3] "hsa-let-7a-2* ?MIMAT0010195 Homo sapiens let-7a-2*" >> as.vector(A) > [1] "hsa-let-7a ?MIMAT0000062 Homo sapiens let-7a" > [2] "hsa-let-7a* ?MIMAT0004481 Homo sapiens let-7a*" > [3] "hsa-let-7a-2* ?MIMAT0010195 Homo sapiens let-7a-2*" >> > I would like to extract only the first field (of variable length). That is I need a vector containing > "hsa-let-7a " > "hsa-let-7a*" > "hsa-let-7a-2*" > > The operator [[]][] works only on the single vector element. I would like to extract the 1st field > with one single instruction rather than a loop as traditional programming languages request. > > Thank you in advance for you help. > Maura > > > > tutti i telefonini TIM! > > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >