Hi all, I have a file, say, test.txt, which contains the following information. I'm trying to read in the file and specifying the missing values as NA so that each column has the same number of rows. I've tried all sorts of manipulation but to no avail. r1 r2 r3 1 3 2 3 3 2 3 4 2 3 5 2 3 6 2 3 7 2 8 2 9 2 3 The output should be r1 r2 r3 1 NA 3 2 NA 3 3 2 3 4 2 3 5 2 3 6 2 3 7 2 NA 8 2 NA 9 2 3 Muhammad
What is the delimiter between the columns? If it is a tab/comma, then read.table will handle it. If as your example shows, the missing data is just a space, then you will have to have some code that cleans up the data, For example a single space is replaced by a single comma, two spaces replaced by two commas, ... Then read.csv should handle it fine. On Thu, May 6, 2010 at 12:10 PM, Muhammad Rahiz < muhammad.rahiz@ouce.ox.ac.uk> wrote:> Hi all, > > I have a file, say, test.txt, which contains the following information. I'm > trying to read in the file and specifying the missing values as NA so that > each column has the same number of rows. > > I've tried all sorts of manipulation but to no avail. > > r1 r2 r3 > 1 3 > 2 3 > 3 2 3 > 4 2 3 > 5 2 3 > 6 2 3 > 7 2 > 8 2 > 9 2 3 > > The output should be > > r1 r2 r3 > 1 NA 3 > 2 NA 3 > 3 2 3 > 4 2 3 > 5 2 3 > 6 2 3 > 7 2 NA > 8 2 NA > 9 2 3 > > Muhammad > > ______________________________________________ > 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<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? [[alternative HTML version deleted]]
On May 6, 2010, at 12:10 PM, Muhammad Rahiz wrote:> Hi all, > > I have a file, say, test.txt, which contains the following > information. I'm trying to read in the file and specifying the > missing values as NA so that each column has the same number of rows. > > I've tried all sorts of manipulation but to no avail. > > r1 r2 r3 > 1 3 > 2 3 > 3 2 3 > 4 2 3 > 5 2 3 > 6 2 3 > 7 2 > 8 2 > 9 2 3 >If you rplace the "," between the header labels with "\t" then read.fwf will do it: > read.fwf(textConnection("r1\tr2\tr3 + 1 3 + 2 3 + 3 2 3 + 4 2 3 + 5 2 3 + 6 2 3 + 7 2 + 8 2 + 9 2 3"), header=TRUE, widths=c(1,-1,1,-1,1) ) r1 r2 r3 1 1 NA 3 2 2 NA 3 3 3 2 3 4 4 2 3 5 5 2 3 6 6 2 3 7 7 2 NA 8 8 2 NA 9 9 2 3> The output should be > > r1 r2 r3 > 1 NA 3 > 2 NA 3 > 3 2 3 > 4 2 3 > 5 2 3 > 6 2 3 > 7 2 NA > 8 2 NA > 9 2 3 > > Muhammad > > ______________________________________________ > 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.David Winsemius, MD West Hartford, CT