ExploreR
ExploreR

Reputation: 343

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

I want to import a txt-file into R which holds some complex numbers. The dataset has a header and is whitespace-separated, decimals are point-separated. Following an impression how the dataset looks like:

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

I have had a few attempts to import the dataset but I failed, I lost precision of the numbers stored in the txt-file. Does anyone know how to import a txt-file into R preserving complex numbers?

#--------------------------------------------------------------------------------------------------
# 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

Upvotes: 0

Views: 199

Answers (2)

ExploreR
ExploreR

Reputation: 343

Just like @Paul wrote in a comment:

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

works fine! But it is more a printing phenomenon, easily to solve with:

base::options(digits = 20)

(This will just print more digits than per default). Thanks a lot for your help, @Paul ! :)

Upvotes: 0

tpetzoldt
tpetzoldt

Reputation: 5813

The data can be read as usual with read.table and others from a text file or from inline text as in the following example:

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 ...

and if the complexity is clear from the values, without the colClassesoption:

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

If your data are on an external file, replace argument text with a file name.

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

and if the column types are mixed use a vector of colClasses or automatic detection.

Upvotes: 1

Related Questions