【发布时间】:2021-08-09 13:33:54
【问题描述】:
我想将一个 txt 文件导入到 R 中,其中包含一些复数。数据集有一个标题并且是空格分隔的,小数是点分隔的。印象中数据集的样子:
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 文件中的数字的精度。 有谁知道如何将 txt 文件导入 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
【问题讨论】:
-
嗨,我通过
df <- read.table(file = "./a_folder/test.txt", header = TRUE)成功导入了关于精度的信息,您看过stackoverflow.com/questions/31383951/… 吗? -
您的示例不包含任何复数,我希望其形式为“2i+3”或类似的形式。它们是如何存储在文本文件中的?
标签: r import complex-numbers base txt