Re: [R] Text search
Hi, > dat$new[grep("Tall", dat$char)] <- "Tall" > dat$new[grep("Short", dat$char)] <- "Short" > dat obs Year charnew 1 1 2001 Tall156 Tall 2 2 2002 12565Tall Tall 3 3 2003 all54 4 4 2004 Short Short 5 5 2005 54all 6 6 2006 7Short12 Short Assuming that no entries contain both Tall and Short. Sarah On Sat, Jan 25, 2020 at 6:23 PM Ashta wrote: > > Hi all, > From one of the columns of the data frame I want to search and > extract a text that contains Tall or Short and create new column > that should contain these texts in a corresponding row. > My example data and the desired output are shown below > > dat<-read.table(text="obs Year char > 1 2001 Tall156 > 2 2002 12565Tall > 3 2003 all54 > 4 2004 Short > 5 2005 54all > 6 2006 7Short12 ",header=TRUE,stringsAsFactors=F) > dat$new <- " " > > Desired out put > obs Year charnew >1 2001 Tall156 Tall >2 2002 12565TallTall >3 2003 all54 >5 2004 Short Short >6 2005 Shall54 >7 2006 7Short12 Short > > How do I get my desired output? > Thank you. > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. -- Sarah Goslee (she/her) http://www.numberwright.com __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
[R] Text search
Hi all, >From one of the columns of the data frame I want to search and extract a text that contains Tall or Short and create new column that should contain these texts in a corresponding row. My example data and the desired output are shown below dat<-read.table(text="obs Year char 1 2001 Tall156 2 2002 12565Tall 3 2003 all54 4 2004 Short 5 2005 54all 6 2006 7Short12 ",header=TRUE,stringsAsFactors=F) dat$new <- " " Desired out put obs Year charnew 1 2001 Tall156 Tall 2 2002 12565TallTall 3 2003 all54 5 2004 Short Short 6 2005 Shall54 7 2006 7Short12 Short How do I get my desired output? Thank you. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] text search in r
Hi, One more solution: new<-"BABBBABB" resAB<-count(unlist(strsplit(gsub("AB",1,new),""))==1)[2,] resAB # x freq #2 TRUE 3 resBA<-count(unlist(strsplit(gsub("BA",2,new),""))==2)[2,] resBA # x freq #2 TRUE 2 new1<-c("ABABAAABABBABABBA") resAB<-count(unlist(strsplit(gsub("AB",1,new1),""))==1)[2,] resAB # x freq #2 TRUE 7 resBA<-count(unlist(strsplit(gsub("BA",2,new1),""))==2)[2,] resBA # x freq #2 TRUE 7 A.K. - Original Message - From: rnewbie565 To: r-help@r-project.org Cc: Sent: Thursday, August 2, 2012 5:27 PM Subject: [R] text search in r I am trying to count the number of times that the characters in a string change For example- new=c(BABBBABB) I want to find the number of times that B changes to A and the number of times that A changes to B. I tried the grep command but I only figured out the positions of when B changes to A when I only need the number of times it occurs. -- View this message in context: http://r.789695.n4.nabble.com/text-search-in-r-tp4638961.html Sent from the R help mailing list archive at Nabble.com. __ 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. __ 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.
Re: [R] text search in r
Hi, You could also use these: library(Biostrings) gregexpr2("AB",new) #[[1]] #[1] 4 10 14 length(unlist(gregexpr2("AB",new))) #[1] 3 length(unlist(gregexpr2("BA",new))) #[1] 2 matchPattern("AB",new) # Views on a 16-letter BString subject #subject: BABBBABB #views: # start end width #[1] 4 5 2 [AB] #[2] 10 11 2 [AB] #[3] 14 15 2 [AB] res1<-matchPattern("AB",new) length(res1) #[1] 3 res2<-matchPattern("BA",new) length(res2) #[1] 2 #another string new1<-c("ABABAAABABBABABBA") res2<-matchPattern("BA",new1) length(res2) #[1] 7 length(unlist(gregexpr2("BA",new1))) #[1] 7 A.K. - Original Message ----- From: rnewbie565 To: r-help@r-project.org Cc: Sent: Thursday, August 2, 2012 5:27 PM Subject: [R] text search in r I am trying to count the number of times that the characters in a string change For example- new=c(BABBBABB) I want to find the number of times that B changes to A and the number of times that A changes to B. I tried the grep command but I only figured out the positions of when B changes to A when I only need the number of times it occurs. -- View this message in context: http://r.789695.n4.nabble.com/text-search-in-r-tp4638961.html Sent from the R help mailing list archive at Nabble.com. __ 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. __ 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.
Re: [R] text search in r
There's no need for 'strsplit' here. new=c("BABBBABB") A2B <- gregexpr("AB", new) B2A <- gregexpr("BA", new) length(A2B[[1]]) #3 length(B2A[[1]]) #2 Cheers, Eloi On 12-08-02 03:44 PM, David Winsemius wrote: On Aug 2, 2012, at 2:27 PM, rnewbie565 wrote: I am trying to count the number of times that the characters in a string change For example- new=c(BABBBABB) Presumably you meant to quote that string. I want to find the number of times that B changes to A and the number of times that A changes to B. I tried the grep command but I only figured out the positions of when B changes to A when I only need the number of times it occurs. > new=c('BABBBABB') > unlist(strsplit(new,"")) [1] "A" "A" "A" "A" "B" "B" "B" "B" "B" "A" "B" "B" "B" "A" "B" "B" > rle(unlist(strsplit(new,""))) Run Length Encoding lengths: int [1:6] 4 5 1 3 1 2 values : chr [1:6] "A" "B" "A" "B" "A" "B" > paste0( rle(unlist(strsplit(new,"")))$values, collapse="") [1] "ABABAB" > gregexpr("AB", .Last.value) [[1]] [1] 1 3 5 attr(,"match.length") [1] 2 2 2 attr(,"useBytes") [1] TRUE So the length of that list value could be used for the AB transitions. Similarly for the BA ones > strrle <- paste0( rle(unlist(strsplit(new,"")))$values, collapse="") > length( gregexpr("BA", strrle)[[1]] ) [1] 2 -- View this message in context: http://r.789695.n4.nabble.com/text-search-in-r-tp4638961.html Sent from the R help mailing list archive at Nabble.com. __ 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. David Winsemius, MD Alameda, CA, USA __ 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. -- Eloi Mercier Bioinformatics PhD Student, UBC Paul Pavlidis Lab 2185 East Mall University of British Columbia Vancouver BC V6T1Z4 __ 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.
Re: [R] text search in r
On Aug 2, 2012, at 2:27 PM, rnewbie565 wrote: I am trying to count the number of times that the characters in a string change For example- new=c(BABBBABB) Presumably you meant to quote that string. I want to find the number of times that B changes to A and the number of times that A changes to B. I tried the grep command but I only figured out the positions of when B changes to A when I only need the number of times it occurs. > new=c('BABBBABB') > unlist(strsplit(new,"")) [1] "A" "A" "A" "A" "B" "B" "B" "B" "B" "A" "B" "B" "B" "A" "B" "B" > rle(unlist(strsplit(new,""))) Run Length Encoding lengths: int [1:6] 4 5 1 3 1 2 values : chr [1:6] "A" "B" "A" "B" "A" "B" > paste0( rle(unlist(strsplit(new,"")))$values, collapse="") [1] "ABABAB" > gregexpr("AB", .Last.value) [[1]] [1] 1 3 5 attr(,"match.length") [1] 2 2 2 attr(,"useBytes") [1] TRUE So the length of that list value could be used for the AB transitions. Similarly for the BA ones > strrle <- paste0( rle(unlist(strsplit(new,"")))$values, collapse="") > length( gregexpr("BA", strrle)[[1]] ) [1] 2 -- View this message in context: http://r.789695.n4.nabble.com/text-search-in-r-tp4638961.html Sent from the R help mailing list archive at Nabble.com. __ 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. David Winsemius, MD Alameda, CA, USA __ 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.
[R] text search in r
I am trying to count the number of times that the characters in a string change For example- new=c(BABBBABB) I want to find the number of times that B changes to A and the number of times that A changes to B. I tried the grep command but I only figured out the positions of when B changes to A when I only need the number of times it occurs. -- View this message in context: http://r.789695.n4.nabble.com/text-search-in-r-tp4638961.html Sent from the R help mailing list archive at Nabble.com. __ 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.