[英]Excel misses values from numeric vector with write.table function in R
I am trying to save a data.frame from R so that it can be read from Excel. 我正在尝试从R保存data.frame,以便可以从Excel中读取它。 I have done this with several other data.frames that have the same structure as the one I refer to now, so far without problems.
我已经用其他几个data.frame完成了此操作,它们的结构与我现在所指的结构相同,到目前为止没有问题。 But for some reason when I try to save this data.frame and then open it with Excel, many of the numerical values in the columns FreqDev and LengthDev are not read by Excel.
但是由于某些原因,当我尝试保存此data.frame然后使用Excel打开它时,Excel不会读取FreqDev和LengthDev列中的许多数值。 Instead, the rows show a string of "#" symbols.
而是,行显示一串“#”符号。
My data.frame looks like this: 我的data.frame看起来像这样:
head(RegPartV)
LogFreq Word PhonCV WordClass FreqDev LengthDev Irregular
1277 28.395 geweest CV-CVVCC V 5.464336 -1.1518498 FALSE
903 25.647 gemaakt CV-CVVCC V 4.885296 -1.1518498 FALSE
752 23.304 gehad CV-CVC V 4.391595 -2.1100420 FALSE
610 22.765 gebracht CV-CCVCC V 4.278021 -0.6727537 FALSE
1312 22.041 gezegd CV-CVCC V 4.125465 -1.6309459 FALSE
647 21.987 gedaan CV-CVVC V 4.114086 -1.6309459 FALSE
The type of information in the data.frame is: data.frame中的信息类型为:
str(RegPartV)
'data.frame': 2096 obs. of 7 variables:
$ LogFreq : num 28.4 25.6 23.3 22.8 22 ...
$ Word : chr "geweest" "gemaakt" "gehad" "gebracht" ...
$ PhonCV : chr "CV-CVVCC" "CV-CVVCC" "CV-CVC" "CV-CCVCC" ...
$ WordClass: Factor w/ 1 level "V": 1 1 1 1 1 1 1 1 1 1 ...
$ FreqDev : num 5.46 4.89 4.39 4.28 4.13 ...
$ LengthDev: num -1.152 -1.152 -2.11 -0.673 -1.631 ...
$ Irregular: logi FALSE FALSE FALSE FALSE FALSE FALSE ...
What is strange is that if I put my mouse over the numerical cells that now have only # symbols (in the excel file), I see a trace of the numbers that used to be there in the original R data.frame. 奇怪的是,如果将鼠标放在现在只有#个符号的数字单元上(在excel文件中),我会看到原始R data.frame中曾经存在的数字的痕迹。 For example, the values of these columns for the first row in the data.frame are:
例如,data.frame中第一行的这些列的值为:
>RegPartV[1,c(5,6)]
FreqDev LengthDev
1277 5.464336 -1.15185
And if I put my mouse over the Excel cells (that contain only # symbols) corresponding to the same values I just showed, I see: 而且,如果将鼠标放在与刚刚显示的相同值对应的Excel单元格(仅包含#个符号)上,我会看到:
54643356148468
and 和
-115184982188519
So the numbers are still there, but for some reason either R or Excel lost count of where the decimal was. 因此数字仍然存在,但是由于某种原因,R或Excel都无法计算小数点所在的位置。
The method I am using to save the data.frame (and that I've used for structurally equivalent data.frame) is: 我用来保存data.frame(以及用于结构上等效的data.frame)的方法是:
write.table(RegPartV,file="RegPartV",quote=F,sep="\t",row.names=F,col.names=T)
Then I open the file with Excel and I would expect to see all the info there, for some reason I'm having this numeric problem with this particular data.frame. 然后,我使用Excel打开文件,由于某些原因,我在使用此特定的data.frame时遇到了数字问题,因此我希望可以在其中看到所有信息。
Any suggestions to get an Excel-readable data.frame are very welcome. 非常欢迎获得Excel可读的data.frame的任何建议。
Thanks in advance. 提前致谢。
From your problem description I suspect that you have "," as the default decimal separator in Excel. 从问题描述中,我怀疑您在Excel中将“,”作为默认的十进制分隔符。 Either change the default in Excel or add
dec=","
to the write.table
command. 可以在Excel中更改默认值
dec=","
可以在write.table
命令中添加dec=","
。
That isn't actually an error: "#" means that a string/value is too long to fit into column. 这实际上不是错误:“#”表示字符串/值太长而无法放入列中。 Widen the column and you'll see proper contents.
加宽列,您将看到正确的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.