8

I study R with R Cookbook 2nd edition. in 4.6 Reading Fixed-Width Records, I typed code as written in book. but my code doesn't work like book

Fisher    R.A.      1890 1962
Pearson   Karl      1857 1936
Cox       Gertrude  1900 1978
Yates     Frank     1902 1994
Smith     Kirstine  1878 1939

save this lines as "fixed-width.fwf" and "fixed-width.txt" both

and run code

f1 = "fixed-width.fwf"
f2 = "fixed-width.txt"

t1 <- read_fwf(f1, fwf_empty(f1, col_names = c("last", "first", "birth", "death")))

t2 <- read_fwf(f2, fwf_empty(f2, col_names = c("last", "first", "birth", "death")))

t1 and t2 both print this error message

Error: `file` must be a regular file, not a connection

sorry for my low level English

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 4
    Did you find a solution? I receive the same error on code that once worked. – Richard Herron Feb 18 '22 at 13:58
  • I had the same error message from reading a simple text file. I found that using fwf_widths() to define the column names and widths resolved the problem. – David Oct 21 '22 at 01:33

1 Answers1

0

Have you got a file named 'fixed-width.txt' in your working directory?

Running the below code (which pretty much matches what you've provided above) creates the desired output for me:

library("tidyverse")

write_lines(
"Fisher    R.A.      1890 1962
Pearson   Karl      1857 1936
Cox       Gertrude  1900 1978
Yates     Frank     1902 1994
Smith     Kirstine  1878 1939",
file = "./fixed-width.txt")

In_txt_file <- "./fixed-width.txt"

In_Data <- read_fwf(In_txt_file, fwf_empty(In_txt_file, col_names = c("Last_Name", "First_Name", "Birth_Date", "Death_Date")))