Hi All, I have a dataset that I would like to split based on : or ? ( the data file is tab delimited) for example: Ny:23-45 AC BA:88-91 DB KJ:21-13 PA And I would like the data to be splitted and the final results look like NY 23 45 AC BA 88 91 DB KJ 21 13 PA I would like to have the resulting data as a data frame so each column is a variable. Thanks, -- View this message in context: http://r.789695.n4.nabble.com/help-splitting-a-data-frame-tp2306832p2306832.html Sent from the R help mailing list archive at Nabble.com.
Try this: # Lines <- readLines('your_file') Lines <- "Ny:23-45 AC BA:88-91 DB KJ:21-13 PA" DF <- read.table(textConnection(gsub("[-:]", "\t", Lines))) closeAllConnections() On Thu, Jul 29, 2010 at 2:34 PM, kayj <kjaja27@yahoo.com> wrote:> > Hi All, > > I have a dataset that I would like to split based on : or – ( the data file > is tab delimited) for example: > Ny:23-45 AC > BA:88-91 DB > KJ:21-13 PA > > And I would like the data to be splitted and the final results look like > NY 23 45 AC > BA 88 91 DB > KJ 21 13 PA > > I would like to have the resulting data as a data frame so each column is a > variable. > > Thanks, > > > -- > View this message in context: > http://r.789695.n4.nabble.com/help-splitting-a-data-frame-tp2306832p2306832.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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
strsplit(s, split=":") anyhoo ?strsplit should get you started On Thu, Jul 29, 2010 at 1:34 PM, kayj <kjaja27 at yahoo.com> wrote:> > Hi All, > > I have a dataset that I would like to split based on : or ? ( the data file > is tab delimited) for example: > Ny:23-45 ? ? ? ?AC > BA:88-91 ? ? ? ?DB > KJ:21-13 ? ? ? ?PA > > And I would like the data to be splitted and the final results look like > NY ? ? ?23 ? ? ?45 ? ? ?AC > BA ? ? ?88 ? ? ?91 ? ? ?DB > KJ ? ? ?21 ? ? ?13 ? ? ?PA > > I would like to have the resulting data as a data frame so each column is a > variable. > > Thanks, > > > -- > View this message in context: http://r.789695.n4.nabble.com/help-splitting-a-data-frame-tp2306832p2306832.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Stephen Sefick ____________________________________ | Auburn University? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? | | Department of Biological Sciences? ? ? ? ?? | | 331 Funchess Hall? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | | Auburn, Alabama? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? | | 36849? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | |___________________________________| | sas0025 at auburn.edu? ? ? ? ? ? ? ? ? ? ? ? ? ?? | | http://www.auburn.edu/~sas0025? ? ? ? ? ?? | |___________________________________| Let's not spend our time and resources thinking about things that are so little or so large that all they really do for us is puff us up and make us feel like gods.? We are mammals, and have not exhausted the annoying little problems of being mammals. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? -K. Mullis
On Thu, 29 Jul 2010, kayj wrote:> > Hi All, > > I have a dataset that I would like to split based on : or ? ( the data file > is tab delimited) for example: > Ny:23-45 AC > BA:88-91 DB > KJ:21-13 PA > > And I would like the data to be splitted and the final results look like > NY 23 45 AC > BA 88 91 DB > KJ 21 13 PAread.table(textConnection(gsub("[-:]","\t",readLines("your.data.file.tab")))) or read.table(textConnection(chartr("-:","\t\t",readLines("your.data.file.tab")))) HTH, Chuck> > I would like to have the resulting data as a data frame so each column is a > variable. > > Thanks, > > > -- > View this message in context: http://r.789695.n4.nabble.com/help-splitting-a-data-frame-tp2306832p2306832.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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