R read.table error: line 1 did not have 52 elements
I am trying to read weather data from the German Weather Service (DWD). Here is a small portion of the .txt-File:
KL02222200001010000101061101131101471101224 661 321 344 30 1 411 551 571 524 34 6 55 6 56 6 734 904 904 844 8941004 994 964 891 991 99116 2120 1132 21 174 81-99-99 81-99-99 81-99-99 804 001 83 43 53 11 11 12 11 01 13 061 1861 461 2261 001 001 501-9999-99999-99999
KL02222200001020000101731101631101591101654 911 241 674 15 1 321 891 621 614 31 6 67 6 53 6 764 834 834 814 984 734 884 864 981 731 87116 2116 2116 31 234 71-99-99 71-99-99 71-99-99 704 2211 73 83 83 11 11 11 01 11 13 001 001 001 001 001 001 701-9999-99999-99999
KL02222200001030000101371101211100991101194 821 581 244 52 1 651 751 641 674 51 6 55 6 49 6 784 774 774 774 814 744 804 784 811 741 80116 3120 4116 31 334 71-99-99 71-99-99 71-99-99 704 001 81 81 81 11 11 11 01 01 01 001 001 001 2461 001 0011001-9999-99999-99999
I try to import this file using the following code:
df = read.table("myfile.txt", header=FALSE)
However, I receive the following error message:
Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec, : line 1 did not have 52 elements
I tried the first suggestion from this question, but to no avail. Pasting the whole table to the read.table-option text="..."
is impossible as the original data contains over six thousand lines.
Solution 1:
read.table(text="KL02222200001010000101061101131101471101224 661 321 344 30 1 411 551 571 524 34 6 55 6 56 6 734 904 904 844 8941004 994 964 891 991 99116 2120 1132 21 174 81-99-99 81-99-99 81-99-99 804 001 83 43 53 11 11 12 11 01 13 061 1861 461 2261 001 001 501-9999-99999-99999
KL02222200001020000101731101631101591101654 911 241 674 15 1 321 891 621 614 31 6 67 6 53 6 764 834 834 814 984 734 884 864 981 731 87116 2116 2116 31 234 71-99-99 71-99-99 71-99-99 704 2211 73 83 83 11 11 11 01 11 13 001 001 001 001 001 001 701-9999-99999-99999
KL02222200001030000101371101211100991101194 821 581 244 52 1 651 751 641 674 51 6 55 6 49 6 784 774 774 774 814 744 804 784 811 741 80116 3120 4116 31 334 71-99-99 71-99-99 71-99-99 704 001 81 81 81 11 11 11 01 01 01 001 001 001 2461 001 0011001-9999-99999-99999",
as.is=T, sep = "", head=F, strip.white = T, fill=T)
Result (converted to tibble
for better readability)
A tibble: 3 x 52
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12
<chr> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
1 KL022222~ 661 321 344 30 1 411 551 571 524 34 6
2 KL022222~ 911 241 674 15 1 321 891 621 614 31 6
3 KL022222~ 821 581 244 52 1 651 751 641 674 51 6
# ... with 40 more variables: V13 <int>, V14 <int>, V15 <int>, V16 <int>,
# V17 <int>, V18 <int>, V19 <int>, V20 <int>, V21 <int>, V22 <int>,
# V23 <int>, V24 <int>, V25 <int>, V26 <int>, V27 <int>, V28 <int>,
# V29 <int>, V30 <int>, V31 <chr>, V32 <chr>, V33 <chr>, V34 <chr>,
# V35 <int>, V36 <int>, V37 <int>, V38 <int>, V39 <int>, V40 <int>,
# V41 <int>, V42 <int>, V43 <int>, V44 <int>, V45 <int>, V46 <int>,
# V47 <int>, V48 <int>, V49 <int>, V50 <int>, V51 <chr>, V52 <chr>