出于某种莫名其妙的原因,read.fwf颠倒了我正在读入的表中所有数字的符号。我的代码是这样的:
person.widths <- c(6, 8, 3, 6, rep(7, 8), 6, 12)
meas <- - read.fwf(file=myfile,
header=FALSE,
widths=person.widths,
colClasses=c(rep("numeric", 14)),
na.strings='.'
)生成的数据框如下所示:
head(meas)
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14
1 -1 -3.16 -1 -6 -22 -0.79 -0.71 0.48 -0.70 0.44 0 -0.88 -236 -22227190
2 -2 -4.87 -1 -6 -24 -1.15 -1.23 -0.27 -0.96 0.03 0 -0.76 -592 -22227199
3 -3 -2.59 -1 -6 -21 -0.72 -0.32 1.39 -0.34 1.32 0 -0.90 -592 -22227200
4 -4 0.00 2 0 0 0.00 -1.00 0.00 -1.00 0.00 0 0.00 -206 -22227201
5 -5 -3.87 -1 -6 -23 -0.90 -1.49 -0.65 -1.38 -0.35 0 -0.84 -506 -22227202
6 -6 -5.72 0 -6 -25 -1.52 -1.00 0.00 -1.00 0.00 0 0.00 -5859 -22227204但是原始数据是这样的:
> foo <- readLines(myfile)
> head(foo)
[1] " 1 3.16 1 6 22 0.79 0.71 -0.48 0.70 -0.44 0.00 0.88 236 22227190"
[2] " 2 4.87 1 6 24 1.15 1.23 0.27 0.96 -0.03 0.00 0.76 592 22227199"
[3] " 3 2.59 1 6 21 0.72 0.32 -1.39 0.34 -1.32 0.00 0.90 592 22227200"
[4] " 4 0.00 -2 0 0 0.00 1.00 0.00 1.00 0.00 0.00 0.00 206 22227201"
[5] " 5 3.87 1 6 23 0.90 1.49 0.65 1.38 0.35 0.00 0.84 506 22227202"
[6] " 6 5.72 0 6 25 1.52 1.00 0.00 1.00 0.00 0.00 0.00 5859 22227204"这对我来说没有任何意义。在这种情况下,我可以使用一个普通的read.table,它工作正常,但我不知道为什么我使用read.fwf时会切换符号。有谁知道吗?
发布于 2017-04-17 09:03:06
当然,这只是一个简单的打字错误。在队伍中
meas <- - read.fwf(file=myfile,通过在<-赋值运算符之后和实际函数read.fwf()之前挂起这个杂乱的-,可以应用显式的* (-1)乘法。
我们都有过这样的经历。优秀的编辑器可以帮助进行适当的颜色突出显示。
https://stackoverflow.com/questions/43443778
复制相似问题