I have a situation where I'm filling out a dataframe from a database. Sometimes the database query doesn't get anything, so I end up trying to place NULL in the dataframe like below.> temp <- NULL > xmat <- as.data.frame(matrix(NA, 2, 3)) > xmat[1, 2:3] <- tempError in if (m < n * p && (n * p)%%m) stop(gettextf("replacement has %d items, need %d", : missing value where TRUE/FALSE needed I can't get the programme to accept that sometimes what the query looks for just doesn't exist, and I just want to move on to the next calculation leaving the dataframe with a missing value in the given cell. It's a real show stopper and I haven't found a way round it. Best wishes, Mikkel PS. I'm using dbGetQuery to query an SQLite database.
On Thu, 2005-07-07 at 10:20 -0700, Mikkel Grum wrote:> I have a situation where I'm filling out a dataframe > from a database. Sometimes the database query doesn't > get anything, so I end up trying to place NULL in the > dataframe like below. > > > temp <- NULL > > xmat <- as.data.frame(matrix(NA, 2, 3)) > > xmat[1, 2:3] <- temp > Error in if (m < n * p && (n * p)%%m) > stop(gettextf("replacement has %d items, need %d", : > missing value where TRUE/FALSE needed > > I can't get the programme to accept that sometimes > what the query looks for just doesn't exist, and I > just want to move on to the next calculation leaving > the dataframe with a missing value in the given cell. > It's a real show stopper and I haven't found a way > round it. > > Best wishes, > Mikkel > > PS. I'm using dbGetQuery to query an SQLite database.NULL represents a zero length object in R. Thus, trying to set only the first row in a data frame to NULL makes no sense, since you cannot have a 0 length object that also has a single row (as you seem to be trying to do above). Since a data frame is a series of lists, you could do the following:> temp <- NULL > xmat <- as.data.frame(matrix(NA, 2, 3))> xmatV1 V2 V3 1 NA NA NA 2 NA NA NA> xmat[, 1] <- temp> xmatV2 V3 1 NA NA 2 NA NA which removes the first column in the data frame. This is the same as:> xmat[, -1]V2 V3 1 NA NA 2 NA NA You could also set the entire xmat to NULL as follows:> xmatV1 V2 V3 1 NA NA NA 2 NA NA NA> xmat <- NULL> xmatNULL You can then test to see if 'xmat' is a NULL:> is.null(xmat)[1] TRUE and base a boolean expression and resultant action on that result: if (!is.null(xmat)) { do_calculations... } If your calculations are on a row by row basis, where NA's represent missing data, you can also use one of several functions to eliminate those rows. See ?na.action, ?na.omit and ?complete.cases for more information and examples. HTH, Marc Schwartz
Mikkel Grum wrote:> I have a situation where I'm filling out a dataframe > from a database. Sometimes the database query doesn't > get anything, so I end up trying to place NULL in the > dataframe like below. > > >>temp <- NULL >>xmat <- as.data.frame(matrix(NA, 2, 3)) >>xmat[1, 2:3] <- temp > > Error in if (m < n * p && (n * p)%%m) > stop(gettextf("replacement has %d items, need %d", : > missing value where TRUE/FALSE needed > > I can't get the programme to accept that sometimes > what the query looks for just doesn't exist, and I > just want to move on to the next calculation leaving > the dataframe with a missing value in the given cell. > It's a real show stopper and I haven't found a way > round it.See ?try Uwe Ligges> Best wishes, > Mikkel > > PS. I'm using dbGetQuery to query an SQLite database. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Maybe I have it wrong, but I think you merely want: temp <- NA Patrick Burns Burns Statistics patrick at burns-stat.com +44 (0)20 8525 0696 http://www.burns-stat.com (home of S Poetry and "A Guide for the Unwilling S User") Mikkel Grum wrote:>I have a situation where I'm filling out a dataframe >from a database. Sometimes the database query doesn't >get anything, so I end up trying to place NULL in the >dataframe like below. > > > >>temp <- NULL >>xmat <- as.data.frame(matrix(NA, 2, 3)) >>xmat[1, 2:3] <- temp >> >> >Error in if (m < n * p && (n * p)%%m) >stop(gettextf("replacement has %d items, need %d", : > missing value where TRUE/FALSE needed > >I can't get the programme to accept that sometimes >what the query looks for just doesn't exist, and I >just want to move on to the next calculation leaving >the dataframe with a missing value in the given cell. >It's a real show stopper and I haven't found a way >round it. > >Best wishes, >Mikkel > >PS. I'm using dbGetQuery to query an SQLite database. > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > > > > >