0

I can't change the file I was given. Its extension is .txt, and every line looks like this:

00004 A009     1 Cholera, unspecified           Cholera, unspecified

When I try to read it with read.table, it returns only one column. How can I separate the 5 columns? (They have spaces between them, but when I use " " as sep, I get an error)

cmaher
  • 5,100
  • 1
  • 22
  • 34
Celia
  • 41
  • 3
  • 2
    It might be tab separated - try using `"\\t"` as the separator – Andrew Gustar May 11 '18 at 19:25
  • Your data has mixed delimiters? – Dodge May 11 '18 at 19:26
  • Can you give us expected result ? – Wilcar May 11 '18 at 19:28
  • Maybe you could see if this solves it: [Multiple Separators for the same file input R](https://stackoverflow.com/questions/20075135/multiple-separators-for-the-same-file-input-r). It does not seem to be a dupe but it seems to be related. – Rui Barradas May 11 '18 at 19:32
  • 1
    Tab-separated files can be read with `read.delim` or you can use any spreadsheet program to preprocess and save as a CSV file. Another possible source would be a "non-breaking space" which would not be recognized by read.table as a valid "whitespace" character. – IRTFM May 11 '18 at 19:45

1 Answers1

0

Data:

I copy and pasted the your example data to a text file called tester.txt in an attempt to reproduce your error. The file contains nothing more than the following:

00004 A009 1 Cholera, unspecified Cholera, unspecified

Read Table:

I get the following with read.table:

df <- read.table("tester.txt")

> df
  V1   V2 V3       V4          V5       V6          V7
1  4 A009  1 Cholera, unspecified Cholera, unspecified

The output here my not be delineated exactly as you expect but you can quickly modify the columns as needed. It appears that your error is caused by an item in your .txt file not evident in your example.

Modify columns:

You mentioned that you wanted five columns so I am assuming you would like to do the following once you read your .txt file:

df[,4] <- paste0(df$V4,df$V5)
df[,5] <- paste0(df$V6,df$V7)
df <- df[,1:5]

> df
  V1   V2 V3                  V4                  V5
1  4 A009  1 Cholera,unspecified Cholera,unspecified
Dodge
  • 3,219
  • 3
  • 19
  • 38