如何将带有复数的 txt-file 导入 R?

How to import txt-file with complex numbers into R?

我想将 txt-file 导入到包含一些复数的 R 中。数据集有一个 header 并且是 whitespace-separated,小数是 point-separated。印象之后数据集的样子:

a b c
1606315601.36889 -0.0119374750903214 0.0362932827218628
1606940201.38086 -0.0121142788819912 0.0360182146610096
1606210201.38693 -0.0124296203543005 0.0332458188804718
1606336201.3989 -0.0124724358297131 0.0355308140075942
1606312801.41093 -0.0126693799402413 0.0354588503147717

我尝试过几次导入数据集但都失败了,我失去了存储在 txt-file 中的数字的精度。 有谁知道如何将 txt-file 导入 R 并保留复数?

#--------------------------------------------------------------------------------------------------
# 1st attempt

test <- base::as.data.frame(base::matrix(data = base::scan(file = test_dir, skip = 1, sep = '', dec = '.', what = 'complex'), ncol = 3, byrow = TRUE), stringsAsFactors = FALSE)
# read the txt-file and store it as a dataframe

class(test$V1)
# query whether the numbers have been read as complex numbers
[1] "character"

#---------------------------------------------------------------------------------------------------
# 2nd attempt

test <- utils::read.table(file = test_dir, skip = 1, sep = '', dec = '.', numerals = 'no.loss', colClasses = 'complex')
# read the txt-file

base::head(test, n = 5)
# print the first 5rows of the txt-file --> this will just print rounded values

可以像往常一样使用 read.table 和其他文本文件或内联文本读取数据,如下例所示:

df <- read.table(text='
a b c
1606315601.36889 -0.0119374750903214 0.0362932827218628
1606940201.38086 -0.0121142788819912 0.0360182146610096
1606210201.38693 -0.0124296203543005 0.0332458188804718
1606336201.3989 -0.0124724358297131 0.0355308140075942
1606312801.41093 -0.0126693799402413 0.0354588503147717                 
', colClasses="complex", header=TRUE)

str(df)

'data.frame':   5 obs. of  3 variables:
 $ a: cplx  1.61e+09+0i 1.61e+09+0i 1.61e+09+0i ...
 $ b: cplx  -0.0119+0i -0.0121+0i -0.0124+0i ...
 $ c: cplx  0.0363+0i 0.036+0i 0.0332+0i ...

并且如果从值中可以清楚地看出复杂性,则没有 colClasses 选项:

df <- read.table(text = '
a b c
2+5i 6.3  0+1i
1.3  7.8  6.0
',  header=TRUE)

str(df)

'data.frame':   2 obs. of  3 variables:
 $ a: cplx  2+5i 1.3+0i
 $ b: num  6.3 7.8
 $ c: cplx  0+1i 6+0i

如果您的数据位于外部文件中,请将参数 text 替换为文件名。

read.table("file.txt", colClasses="complex", header=TRUE)

如果列类型混合使用colClasses向量或自动检测。

就像@Paul在评论中写的那样:

utils::read.table(file = test_dir, skip = 1, sep = '', dec = '.', numerals = 'no.loss')

工作正常! 但更多的是印刷现象,很容易解决:

base::options(digits = 20)

(这只会打印比默认情况下更多的数字)。非常感谢您的帮助,@Paul! :)