naw3 at duke.edu
2008-Jul-01 17:49 UTC
[R] Ignore blank columns when reading a row from a table
Hi, I am extracting data from a table where the rows have different column lengths, and empty columns have NA in them. Whenever I extract a row with some empty columns, the resulting vector carries all the NAs. Is there a way to ignore the empty columns? Thanks, -Nina
Erik Iverson
2008-Jul-01 18:12 UTC
[R] Ignore blank columns when reading a row from a table
It depends what you mean by 'ignore'. Some functions have an na.rm argument which throws out NAs before computing the statistic. if the vector 'x' has NAs, then x <- x[!is.na(x)] may be what you're looking for. This removes NAs from x and reassigns the value to x. naw3 at duke.edu wrote:> Hi, > > I am extracting data from a table where the rows have different column lengths, > and empty columns have NA in them. Whenever I extract a row with some empty > columns, the resulting vector carries all the NAs. Is there a way to ignore the > empty columns? > > Thanks, > -Nina > > ______________________________________________ > 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.
Jorge Ivan Velez
2008-Jul-01 18:26 UTC
[R] Ignore blank columns when reading a row from a table
Dear Nina, If you have a matrix X like this # Data set set.seed(123) X=matrix(rnorm(10*5),ncol=5) X[1,1]<-NA X[2,1]<-NA X[1,5]<-NA X[5,2]<-NA X and you'd like to remove the NA values for a particular row (for example row 1), you can try something like: X[1,!is.na(X[1,])] Now, if you have a vector "y", you could do: set.seed(123) y=c(NA,rnorm(5),NA) y y[!is.na(y)] HTH, Jorge On Tue, Jul 1, 2008 at 1:49 PM, <naw3@duke.edu> wrote:> Hi, > > I am extracting data from a table where the rows have different column > lengths, > and empty columns have NA in them. Whenever I extract a row with some empty > columns, the resulting vector carries all the NAs. Is there a way to ignore > the > empty columns? > > Thanks, > -Nina > > ______________________________________________ > 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. >[[alternative HTML version deleted]]
Bernardo Rangel Tura
2008-Jul-06 15:07 UTC
[R] Ignore blank columns when reading a row from a table
Em Ter, 2008-07-01 ?s 13:49 -0400, naw3 at duke.edu escreveu:> Hi, > > I am extracting data from a table where the rows have different column lengths, > and empty columns have NA in them. Whenever I extract a row with some empty > columns, the resulting vector carries all the NAs. Is there a way to ignore the > empty columns? > > Thanks, > -NinaNina You can use scan() and argument what, if necessary you can use the argument fill=T to solve a diferent coluns length. use ?scan to get help -- Bernardo Rangel Tura, M.D,MPH,Ph.D National Institute of Cardiology Brazil P.S. I learn this tip read: "Data Manipulation with R"