将数据帧导出到R中的文本文件时出错 [英] Error when exporting dataframe to text file in R
问题描述
我错过了一个文本文件,在if(inherits(X [[j]],data.frame)& ncol(xj)> 1L)
X [[j]]< - as.matrix(X [ j]]):
缺少值,其中TRUE / FALSE需要
我使用了以下命令对于导出:
write.table(df,file ='dfname.txt',sep ='\t')
我不知道问题可能源于什么。至于缺少需要TRUE / FALSE的数据,我只有一列包含TRUE / FALSE值,并且这些值都不丢失。
数据框的内容:
> str(df)
'data.frame':776 obs。 15个变量:
$年龄:因子w / 4级别,A,J,SA:2 2 2 2 2 2 2 2 2 2 ...
$性别:因素w / 2级别F,M:1 1 1 1 2 2 2 2 2 ...
$代表:因素w / 11级别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 ...
$质量:数字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 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 ...
$位置:因素w / 4级Loc1,..:4 4 4 4 4 4 2 2 2 2 ...
$网站:因子w / 6级别A,B,C,...:5 5 5 5 5 5 3 3 3 3 ...
$日期:日期格式:2010-08-302010-08-30...
$记录:int 35 34 39 49 69 38 145 112 125 140 ...
$ SampleID:因子w / 776水平AT1-A-F1,AT1-A-F10,..:525 524 527 528
529 526 111 78
88 110 ...
$ Vir1Inc:logi FALSE FALSE FALSE FALSE FALSE FALSE ...
$月:'data.frame':776 obs。的2个变量:
.. $日期:日期格式:2010-08-302010-08-30...
.. $月:因素w / 19级四月-2011,2010年8月,...:2 2 2 2
2 2 18 18 18 18 ...
我希望我给了足够的/正确的信息...
非常感谢,
Heather
agstudy的解决方案提供了一个很好的快速修复,但是有一个简单的替代/一般解决方案不必在数据框架中指定元素(嵌套):
以下位只是从 agstudy的解决方案获取嵌套的数据框架 dd
:
Month = data.frame(Dates = as.Date(2003-02-01)+ 1:15,
Month = gl ,2,15))
dd< - data.frame(Age = 1:15)
dd $月< - Mont你可以使用 akhilsbehl的 LinearizeNestedList()
函数(其中mrdwab提供 here )来平铺(或线性化)嵌套级别: library(devtools)
source_gist(4205477)#loads函数
ddf< - LinearizeNestedList(dd,LinearizeDataFrames = TRUE)
#ddf现在是一个包含两个元素(年龄和月份)的列表
ddf< - LinearizeNestedList(ddf,LinearizeDataFrames = TRUE)
#ddf现在是一个包含3个元素(年龄,`月/日期和月/月)的列表
ddf< - as.data.frame.list(ddf)
#将扁平化/线性化列表转换为data.frame
ddf
现在是一个没有嵌套的数据框。但是,列名仍然反映了嵌套结构:
name(ddf)
[1]Age月份日期Month.Month
如果你想改变这个(在这种情况下似乎在日期
之前编写的 Month。
可以使用 gsub code>和我从Sacha Epskamp复制的一些正则表达式,以删除列名之前的所有文本, code>
。
names(ddf)< - gsub(。* \\。,,names(ddf) )
名称(ddf)
[1]年龄日期月
现在唯一的事情是像往常一样导出数据框架:
write.table(ddf,file = test.txt)
I am trying to write a dataframe in R to a text file, however it is returning to 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 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.
Contents of the 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
解决方案 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:
The following bit is just copied from agstudy's solution to obtain the nested data.frame dd
:
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:
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:
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 .
.
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")
这篇关于将数据帧导出到R中的文本文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!