I am trying to write a dataframe in R to a text file, however it is returning to following error:
我试着在R的一个文本文件中写一个dataframe,但是它返回到以下错误:
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 for the export:
我使用以下命令导出:
write.table(df, file ='dfname.txt', sep='\t' )
I have no idea what the problem could stem from. As far as "missing data where TRUE/FALSE is needed", I have only one column which contains TRUE/FALSE values, and none of these values are missing.
我不知道这个问题的根源。至于“需要TRUE/FALSE”的“缺失数据”,我只有一个包含TRUE/FALSE值的列,这些值都没有丢失。
Contents of the dataframe:
dataframe内容:
> 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 ...
I hope I've given enough/the right information ...
我希望我已经给了足够的/正确的信息……
Many thanks, Heather
许多谢谢,希瑟
3 个解决方案
#1
4
The solution by agstudy provides a great quick fix, but there is a simple alternative/general solution for which you do not have to specify the element(s) in your data.frame that was(were) nested:
agstudy的解决方案提供了一个快速解决方案,但是有一个简单的替代/通用解决方案,您不必在您的数据a中指定元素(was)。
The following bit is just copied from agstudy's solution to obtain the nested data.frame dd
:
下面的代码是从agstudy的解决方案中复制的,以获取嵌套的数据。
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 akhilsbehl's LinearizeNestedList()
function (which mrdwab made available here) to flatten (or linearize) the nested levels:
您可以使用akhilsbehl的LinearizeNestedList()函数(这里提供的mrdwab)使嵌套的级别(或线性化)变平:
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
is now a data.frame without nesting. However, it's column names still reflect the nested structure:
ddf现在是一个没有嵌套的数据。然而,它的列名仍然反映了嵌套的结构:
names(ddf)
[1] "Age" "Month.Dates" "Month.Month"
If you want to change this (in this case it seems 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 text in the column names before the .
.
如果你想要改变它(在这种情况下,一个月似乎是多余的)。例如,在日期之前,您可以使用gsub和我从Sacha Epskamp复制的正则表达式,在.. ..
names(ddf) <- gsub(".*\\.","",names(ddf))
names(ddf)
[1] "Age" "Dates" "Month"
The only thing left now is exporting the data.frame as usual:
现在唯一剩下的事情就是导出数据。
write.table(ddf, file="test.txt")
#2
5
An example to reproduce the error. I create a nested data.frame:
一个复制错误的例子。我创建了一个嵌套数据。frame:
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 inverstigating, one option to remove the nested data.frame
:
如果没有反向引导,可以选择移除嵌套的数据。
write.table(data.frame(subset(dd,select=-c(Month)),unclass(dd$Month)))
#3
2
Alternatively, you could use the "flatten" function from the jsonlite package to flatten the dataframe before export. It achieves the same result of the other functions mentioned and is much easier to implement.
或者,您可以使用jsonlite包中的“flatten”函数来在导出之前将dataframe压平。它实现了前面提到的其他函数的相同结果,并且更容易实现。
jsonlite::flatten
jsonlite:平
https://rdrr.io/cran/jsonlite/man/flatten.html
https://rdrr.io/cran/jsonlite/man/flatten.html
#1
4
The solution by agstudy provides a great quick fix, but there is a simple alternative/general solution for which you do not have to specify the element(s) in your data.frame that was(were) nested:
agstudy的解决方案提供了一个快速解决方案,但是有一个简单的替代/通用解决方案,您不必在您的数据a中指定元素(was)。
The following bit is just copied from agstudy's solution to obtain the nested data.frame dd
:
下面的代码是从agstudy的解决方案中复制的,以获取嵌套的数据。
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 akhilsbehl's LinearizeNestedList()
function (which mrdwab made available here) to flatten (or linearize) the nested levels:
您可以使用akhilsbehl的LinearizeNestedList()函数(这里提供的mrdwab)使嵌套的级别(或线性化)变平:
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
is now a data.frame without nesting. However, it's column names still reflect the nested structure:
ddf现在是一个没有嵌套的数据。然而,它的列名仍然反映了嵌套的结构:
names(ddf)
[1] "Age" "Month.Dates" "Month.Month"
If you want to change this (in this case it seems 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 text in the column names before the .
.
如果你想要改变它(在这种情况下,一个月似乎是多余的)。例如,在日期之前,您可以使用gsub和我从Sacha Epskamp复制的正则表达式,在.. ..
names(ddf) <- gsub(".*\\.","",names(ddf))
names(ddf)
[1] "Age" "Dates" "Month"
The only thing left now is exporting the data.frame as usual:
现在唯一剩下的事情就是导出数据。
write.table(ddf, file="test.txt")
#2
5
An example to reproduce the error. I create a nested data.frame:
一个复制错误的例子。我创建了一个嵌套数据。frame:
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 inverstigating, one option to remove the nested data.frame
:
如果没有反向引导,可以选择移除嵌套的数据。
write.table(data.frame(subset(dd,select=-c(Month)),unclass(dd$Month)))
#3
2
Alternatively, you could use the "flatten" function from the jsonlite package to flatten the dataframe before export. It achieves the same result of the other functions mentioned and is much easier to implement.
或者,您可以使用jsonlite包中的“flatten”函数来在导出之前将dataframe压平。它实现了前面提到的其他函数的相同结果,并且更容易实现。
jsonlite::flatten
jsonlite:平
https://rdrr.io/cran/jsonlite/man/flatten.html
https://rdrr.io/cran/jsonlite/man/flatten.html