[R] Error with write.table

Peter Ehlers ehlers at ucalgary.ca
Sun Feb 21 23:34:27 CET 2010


On 2010-02-21 15:06, jmgray wrote:
>
>
>> I was trying to save a data frame to an excel file using the following
>> command:
>>
>> write.table(myData, file="myData.csv",sep=",", row.names=F)
>>
>> The command works for some data frames, but for other data frames, I get
>> the
>> following error:
>>
>> Error in if (inherits(X[[j]], "data.frame")&&  ncol(xj)>  1L) X[[j]]<-
>> as.matrix(X[[j]]) :
>>    missing value where TRUE/FALSE needed
>>
>
> I got an identical error message when instead of combining two data frames
> (a,b) with a method such as:
>
>            a<- cbind.data.frame(a,b)
>
> I embedded a data.frame in another, e.g.,
>
>            a$b<- b
>
> In the latter example, I have a column (a$b) that consists of a data frame
> onto itself, which write.table apparently can't handle.
>
> Jesse Gray
> Neurobiology
> Harvard Medical School
>
> Here's my precise error message:
>
> Error in if (inherits(X[[j]], "data.frame")&&  ncol(xj)>  1L) X[[j]]<-
> as.matrix(X[[j]]) :
>    missing value where TRUE/FALSE needed

This is one reason why I'm not fond of the '$' way of accessing
dataframe components. I like neither of your methods for
combining two dataframes. Try instead:

a <- data.frame(a, b) ## no problem (assuming same number of rows)

or

a[, "b"] <- b  ## R tells you that you're trying to do
                   something you shouldn't.

  -Peter Ehlers



More information about the R-help mailing list