Dear list, I'm looking for a way to count the number of "|" within an object. The character "|" is used to separated ids. Assume a data (d) structure like Var NA NA NA NA NA 1 1|2 1|22|45 3 4b|24789 I need to know the maximum number of ids within one object. In this case 3 (1|22|45) Does anybody know a better way? Thanks Mit freundlichen Gr??en Andreas Kunzler ____________________________ Bundeszahn?rztekammer (BZ?K) Chausseestra?e 13 10115 Berlin Tel.: 030 40005-113 Fax: 030 40005-119 E-Mail: a.kunzler at bzaek.de
Try this: sapply(strsplit(as.character(Var$Var), "\\|"), length) On Mon, Jul 5, 2010 at 11:04 AM, Kunzler, Andreas <a.kunzler@bzaek.de>wrote:> Dear list, > > I'm looking for a way to count the number of "|" within an object. > The character "|" is used to separated ids. > > Assume a data (d) structure like > > Var > NA > NA > NA > NA > NA > 1 > 1|2 > 1|22|45 > 3 > 4b|24789 > > I need to know the maximum number of ids within one object. In this case 3 > (1|22|45) > > > Does anybody know a better way? > > Thanks > > Mit freundlichen Grüßen > > Andreas Kunzler > ____________________________ > Bundeszahnärztekammer (BZÄK) > Chausseestraße 13 > 10115 Berlin > > Tel.: 030 40005-113 > Fax: 030 40005-119 > > E-Mail: a.kunzler@bzaek.de > > ______________________________________________ > 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]]
On Jul 5, 2010, at 9:04 AM, Kunzler, Andreas wrote:> Dear list, > > I'm looking for a way to count the number of "|" within an object. > The character "|" is used to separated ids. > > Assume a data (d) structure like > > Var > NA > NA > NA > NA > NA > 1 > 1|2 > 1|22|45 > 3 > 4b|24789 > > I need to know the maximum number of ids within one object. In this case 3 (1|22|45) > > > Does anybody know a better way? > > ThanksPresuming that your column is in a data frame called 'DF', where the 'Var' column is likely imported as a factor:> DFVar 1 <NA> 2 <NA> 3 <NA> 4 <NA> 5 <NA> 6 1 7 1|2 8 1|22|45 9 3 10 4b|24789> max(sapply(strsplit(as.character(DF$Var), split = "\\|"), length))[1] 3 The above uses strsplit() to split each line using the "|" as the split character. Since "|" has a special meaning for regular expressions, it needs to be escaped using the double backslash:> strsplit(as.character(DF$Var), split = "\\|")[[1]] [1] NA [[2]] [1] NA [[3]] [1] NA [[4]] [1] NA [[5]] [1] NA [[6]] [1] "1" [[7]] [1] "1" "2" [[8]] [1] "1" "22" "45" [[9]] [1] "3" [[10]] [1] "4b" "24789" Then you just loop through each line getting the length:> sapply(strsplit(as.character(DF$Var), split = "\\|"), length)[1] 1 1 1 1 1 1 2 3 1 2 and of course get the max value. HTH, Marc Schwartz
On Mon, 5 Jul 2010, Kunzler, Andreas wrote:> Dear list, > > I'm looking for a way to count the number of "|" within an object. > The character "|" is used to separated ids. > > Assume a data (d) structure like > > Var > NA > NA > NA > NA > NA > 1 > 1|2 > 1|22|45 > 3 > 4b|24789 > > I need to know the maximum number of ids within one object. In this case 3 (1|22|45) > > > Does anybody know a better way?See ?max ?count.fields and, if you are noit using this on a text file, ?textConnection> count.fields(textConnection("+ Var + NA + NA + NA + NA + NA + 1 + 1|2 + 1|22|45 + 3 + 4b|24789 + "),sep="|") [1] 1 1 1 1 1 1 1 2 3 1 2 HTH, Chuck> > Thanks > > Mit freundlichen Gr??en > > Andreas Kunzler > ____________________________ > Bundeszahn?rztekammer (BZ?K) > Chausseestra?e 13 > 10115 Berlin > > Tel.: 030 40005-113 > Fax: 030 40005-119 > > E-Mail: a.kunzler at bzaek.de > > ______________________________________________ > 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. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901