Gian Luca Negri
2011-Mar-22 19:16 UTC
[R] Data frame and function that requires vector as input
Hi, I have a data.frame(zscores) that looks like this: gA gB g1 0.2 0.6 g2 0.3 Na My problem is that I need to use a function and the output is a vector of only the non NA values, so shorter than the list I would obtain dropping the data.frame. What is the cleanest way to keep row and column names or putting the values back into the same data frame format? The function is fdrtool() and I can compute the values I'm interested like this: FDR=fdrtool([!is.na(zscores)])$qval But at this point I don't know how to track back these values, to the original data frame Thanks, Gian Luca
If a is a vector with missing values and b is the result of the function call that has just the non-missing computations on a, then you can do something like:> newdat <- rep(NA, length(a)) > newdat[ !is.na(a) ] <- bSometimes the which and match functions can be useful as well. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Gian Luca Negri > Sent: Tuesday, March 22, 2011 1:16 PM > To: r-help at r-project.org > Subject: [R] Data frame and function that requires vector as input > > > Hi, > > I have a data.frame(zscores) that looks like this: > > gA gB > g1 0.2 0.6 > g2 0.3 Na > > My problem is that I need to use a function and the output is a vector > of only the non NA values, so shorter than the list I would obtain > dropping the data.frame. > What is the cleanest way to keep row and column names or putting the > values back into the same data frame format? > > The function is fdrtool() and I can compute the values I'm interested > like this: > FDR=fdrtool([!is.na(zscores)])$qval > But at this point I don't know how to track back these values, to the > original data frame > > Thanks, > Gian Luca > > ______________________________________________ > 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
2011-Mar-23 19:48 UTC
[R] Data frame and function that requires vector as input
On Mar 22, 2011, at 3:16 PM, Gian Luca Negri wrote:> > Hi, > > I have a data.frame(zscores) that looks like this: > > gA gB > g1 0.2 0.6 > g2 0.3 NaI hope it doesn't look likethat because it would mean you had gB as a character vector when I think you want that value to be NA.> > My problem is that I need to use a function and the output is a > vector of only the non NA values, so shorter than the list I would > obtain dropping the data.frame. > What is the cleanest way to keep row and column names or putting the > values back into the same data frame format?I sometime create a logical vector in the data.frame named gdval for "good value" > zscores$gdval <- apply(zscores, 1, function(x) all(!is.na(x)) ) > > zscores[ zscores$gdval, -grep("gdval", names(zscores))] gA gB g1 0.2 0.6 # returns all rows without any NA's without removing any data in zscores> > The function is fdrtool() and I can compute the values I'm > interested like this: > FDR=fdrtool([!is.na(zscores)])$qval > But at this point I don't know how to track back these values, to > the original data frame > > Thanks, > Gian Luca > > ______________________________________________ > 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