Dear all, I would like to know how can I compute the length of a string in a dataframe. Example: SEQUENCE ID TGCTCCCATCTCCACGG HR04FS000000645 ACTGAACTCCCATCTCCAAT HR00000595847847 I would like to know how to compute the length of each SEQUENCE. Best regards, João Fadista [[alternative HTML version deleted]]
sLengths <- with(dataFrame, nchar(as.character(SEQUENCE))) Bill Venables CSIRO Laboratories PO Box 120, Cleveland, 4163 AUSTRALIA Office Phone (email preferred): +61 7 3826 7251 Fax (if absolutely necessary): +61 7 3826 7304 Mobile: +61 4 8819 4402 Home Phone: +61 7 3286 7700 mailto:Bill.Venables at csiro.au http://www.cmis.csiro.au/bill.venables/ -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Jo?o Fadista Sent: Wednesday, 5 September 2007 11:51 PM To: r-help at stat.math.ethz.ch Subject: [R] length of a string Dear all, I would like to know how can I compute the length of a string in a dataframe. Example: SEQUENCE ID TGCTCCCATCTCCACGG HR04FS000000645 ACTGAACTCCCATCTCCAAT HR00000595847847 I would like to know how to compute the length of each SEQUENCE. Best regards, Jo?o Fadista [[alternative HTML version deleted]]
Jo?o Fadista wrote:> Dear all, > > I would like to know how can I compute the length of a string in a dataframe. Example: > > SEQUENCE ID > TGCTCCCATCTCCACGG HR04FS000000645 > ACTGAACTCCCATCTCCAAT HR00000595847847 > > I would like to know how to compute the length of each SEQUENCE.?nchar> Best regards, > Jo?o Fadista > > [[alternative HTML version deleted]] > > > > ------------------------------------------------------------------------ > > ______________________________________________ > R-help at stat.math.ethz.ch 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.-- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
How's this?> x = data.frame(ID=c("asdf","asdfasdf"),1:2) > xID X1.2 1 asdf 1 2 asdfasdf 2> nchar(as.character(x$ID))[1] 4 8>Assuming ID is a factor, if not, you can remove the as.character(). On 9/5/07, João Fadista <Joao.Fadista@agrsci.dk> wrote:> > Dear all, > > I would like to know how can I compute the length of a string in a > dataframe. Example: > > SEQUENCE ID > TGCTCCCATCTCCACGG HR04FS000000645 > ACTGAACTCCCATCTCCAAT HR00000595847847 > > I would like to know how to compute the length of each SEQUENCE. > > > Best regards, > João Fadista > > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help@stat.math.ethz.ch 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. > >[[alternative HTML version deleted]]
Hi, sapply(levels(df[,"SEQUENCE"]), nchar) Where 'df' is your data.frame -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O On 05/09/07, João Fadista <Joao.Fadista@agrsci.dk> wrote:> > Dear all, > > I would like to know how can I compute the length of a string in a > dataframe. Example: > > SEQUENCE ID > TGCTCCCATCTCCACGG HR04FS000000645 > ACTGAACTCCCATCTCCAAT HR00000595847847 > > I would like to know how to compute the length of each SEQUENCE. > > > Best regards, > João Fadista > > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help@stat.math.ethz.ch 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. > >[[alternative HTML version deleted]]
On 9/5/07, Jo?o Fadista <Joao.Fadista at agrsci.dk> wrote:> I would like to know how can I compute the length of a string in a dataframe. Example: > > SEQUENCE ID > TGCTCCCATCTCCACGG HR04FS000000645 > ACTGAACTCCCATCTCCAAT HR00000595847847 > > I would like to know how to compute the length of each SEQUENCE.Maybe the following code?> datavar1 var2 1 This is a string 12 2 This is another string 34> nchar(data[,1])[1] 16 22>Paul
On 05-Sep-07 13:50:57, Jo?o Fadista wrote:> Dear all, > > I would like to know how can I compute the length of a string in a > dataframe. Example: > > SEQUENCE ID > TGCTCCCATCTCCACGG HR04FS000000645 > ACTGAACTCCCATCTCCAAT HR00000595847847 > > I would like to know how to compute the length of each SEQUENCE. > > Best regards, > Jo?o Fadistanchar("ACTGAACTCCCATCTCCAAT") [1] 20 seems to work. Find it, and related functions, with help.search("character") As it happens, help.search("string") will not help! Best wishes, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 05-Sep-07 Time: 15:05:22 ------------------------------ XFMail ------------------------------