Error exporting data to text file in R

I am trying to write a dataframe in R to a text file, however it returns to 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 used the following command to export:

 write.table(df, file ='dfname.txt', sep='\t' ) 

I have no idea what the problem is. Regarding the "missing data that requires TRUE / FALSE", I only have one column that contains TRUE / FALSE values, and none of these values ​​are missing.

The contents of the data frame:

 > str(df) 'data.frame': 776 obs. of 15 variables: $ Age : Factor w/ 4 levels "","A","J","SA": 2 2 2 2 2 2 2 2 2 2 ... $ Sex : Factor w/ 2 levels "F","M": 1 1 1 1 2 2 2 2 2 2 ... $ Rep : Factor w/ 11 levels "L","NR","NRF",..: 1 1 4 4 2 2 2 2 2 2 ... $ FA : num 61.5 62.5 60.5 61 59.5 59.5 59.1 59.2 59.8 59.9 ... $ Mass : num 20 19 16.5 17.5 NA 14 NA 23 19 18.5 ... $ Vir1 : num 999 999 999 999 999 999 999 999 999 999 ... $ Vir2 : num 999 999 999 999 999 999 999 999 999 999 ... $ Vir3 : num 40 999 999 999 999 999 999 999 999 999 ... $ Location : Factor w/ 4 levels "Loc1",..: 4 4 4 4 4 4 2 2 2 2 ... $ Site : Factor w/ 6 levels "A","B","C",..: 5 5 5 5 5 5 3 3 3 3 ... $ Date : Date, format: "2010-08-30" "2010-08-30" ... $ Record : int 35 34 39 49 69 38 145 112 125 140 ... $ SampleID : Factor w/ 776 levels "AT1-A-F1","AT1-A-F10",..: 525 524 527 528 529 526 111 78 88 110 ... $ Vir1Inc : logi FALSE FALSE FALSE FALSE FALSE FALSE ... $ Month :'data.frame': 776 obs. of 2 variables: ..$ Dates: Date, format: "2010-08-30" "2010-08-30" ... ..$ Month: Factor w/ 19 levels "Apr-2011","Aug-2010",..: 2 2 2 2 2 2 18 18 18 18 ... 

Hope I have given enough / correct information ...

Thank you very much heather

+6
source share
3 answers

The agstudy solution provides a quick fix, but there is a simple alternative / general solution for which you do not need to specify elements (elements) in your data.frame file that was (were) nested:

The following bit has just been copied from agstudy solution to get the attached .frame dd data:

 Month=data.frame(Dates= as.Date("2003-02-01") + 1:15, Month=gl(12,2,15)) dd <- data.frame(Age=1:15) dd$Month <- Month 

You can use the akhilsbehl LinearizeNestedList() function (which mrdwab provided here ) to smooth out (or linearize) the nested levels:

 library(devtools) source_gist(4205477) #loads the function ddf <- LinearizeNestedList(dd, LinearizeDataFrames = TRUE) # ddf is now a list with two elements (Age and Month) ddf <- LinearizeNestedList(ddf, LinearizeDataFrames = TRUE) # ddf is now a list with 3 elements (Age, `Month/Dates` and `Month/Month`) ddf <- as.data.frame.list(ddf) # transforms the flattened/linearized list into a data.frame 

ddf now a data.frame file without nesting. However, the column names still reflect the nested structure:

 names(ddf) [1] "Age" "Month.Dates" "Month.Month" 

If you want to change this (in this case it is redundant to have Month. Written before Dates , for example), you can use gsub and some regular expression that I copied from Sacha Epskamp to remove all the text in the column names before . .

 names(ddf) <- gsub(".*\\.","",names(ddf)) names(ddf) [1] "Age" "Dates" "Month" 

Now it remains only to export the data.frame file as usual:

 write.table(ddf, file="test.txt") 
+4
source

An example of reproducing an error. I create an attached data.frame file:

 Month=data.frame(Dates= as.Date("2003-02-01") + 1:15, Month=gl(12,2,15)) dd <- data.frame(Age=1:15) dd$Month <- Month str(dd) 'data.frame': 15 obs. of 2 variables: $ Age : int 1 2 3 4 5 6 7 8 9 10 ... $ Month:'data.frame': 15 obs. of 2 variables: ..$ Dates: Date, format: "2003-02-02" "2003-02-03" "2003-02-04" ... ..$ Month: Factor w/ 12 levels "1","2","3","4",..: 1 1 2 2 3 3 4 4 5 5 ... 

No. I try to save it, I reproduce the error:

 write.table(dd) Error in if (inherits(X[[j]], "data.frame") && ncol(xj) > 1L) X[[j]] <- as.matrix(X[[j]]) : missing value where TRUE/FALSE needed 

Without installation, one option to remove nested data.frame :

 write.table(data.frame(subset(dd,select=-c(Month)),unclass(dd$Month))) 
+5
source

Alternatively, you can use the smooth function from the jsonlite package to smooth the data block before exporting. It achieves the same result as other functions, and is much easier to implement.

jsonlite::flatten

https://rdrr.io/cran/jsonlite/man/flatten.html

+2
source

Source: https://habr.com/ru/post/948053/


All Articles