kayj
2008-Oct-03 18:48 UTC
[R] Splitting a file into multiple files based on the column name
Hi, I have a txt file here I want to split it into different files based on the column name if it contains a specific word or part of a word. For example, If the header looks like A001_Sick A015_SB K99_Sick L913_BB I would like to split the data , one file contains the data with column names containing the words ?Sick? or ?SB? and have the rest of the data in another file. I appreciate your help, -- View this message in context: http://www.nabble.com/Splitting-a-file-into-multiple-files-based-on-the-column-name-tp19803535p19803535.html Sent from the R help mailing list archive at Nabble.com.
jim holtman
2008-Oct-04 01:56 UTC
[R] Splitting a file into multiple files based on the column name
You can read the data in, then use 'grep' to determine which rows match, then write the files out: x <- read.table(....) rowIndx <- grep("Sick|CB", x$col) write.table(x[rowIndx,], file="match") write.table(x[-rowIndx,], file="nomatch") On Fri, Oct 3, 2008 at 2:48 PM, kayj <kjaja27 at yahoo.com> wrote:> > Hi, > > I have a txt file here I want to split it into different files based on the > column name if it contains a specific word or part of a word. For example, > > If the header looks like > > A001_Sick A015_SB K99_Sick L913_BB > > I would like to split the data , one file contains the data with column > names containing the words "Sick" or "SB" and have the rest of the data in > another file. > > I appreciate your help, > > > > > > > > -- > View this message in context: http://www.nabble.com/Splitting-a-file-into-multiple-files-based-on-the-column-name-tp19803535p19803535.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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?