Reading table with delimiter = k space with variable k

I have a text file with data separated by spaces. The number of spaces is changing, and I cannot use read.table. You have some tips (ps I'm on the windows).

Two lines from the file:

13001 200901010200 11.49 -23.01 -999.00 46001 200904300200 56.30 -148.00 -999.00 
+6
source share
2 answers

Even when editing, the problem remains unclear. Your example works for me.

 Lines <- "13001 200901010200 11.49 -23.01 -999.00 46001 200904300200 56.30 -148.00 -999.00" con <- textConnection(Lines) x <- read.table(con) close(con) x # V1 V2 V3 V4 V5 # 1 13001 200901010200 11.49 -23.01 -999 # 2 46001 200904300200 56.30 -148.00 -999 

The default value of sep="" works because (as said in ?read.table ):

If 'sep = "(the default value for' Read.table) is a white space separator, that is, one or more spaces, tabs, new lines, or carriage returns.

+5
source

Using sep="" logically equivalent to any number of spaces (in terms of regular expressions "\s+" ).

To read data using read.delim() or read.table() , use:

 read.delim(fileName, sep="") 

It also removes leading spaces (before the first column).

+3
source

Source: https://habr.com/ru/post/891338/


All Articles