Importing a txt file when changing the number of columns?

I am having problems importing a .txt file into R, because the numeric columns vary from eight to nine. Initially, my data has eight columns:

 Date, Open, High, Low, Close, Volume, Open Interest, Delivery Month 

Later I will add an extra Unadjusted close column. How to import data? Somehow the Unadjusted close column Unadjusted close to be ignored at the beginning. I tried

 data1 <- read.table("AD_0.TXT", sep=",", header=TRUE) 

but it does not work.

+6
r text-files
source share
1 answer

You need to use the fill argument in the read.table function. Suppose I have the following file

 "A","B","C" 1,2,3 4,5 6,7,8 

called tmp.txt . Note that line 2 has only two values. Then

 > a = read.table("tmp.txt", sep=",", header=TRUE, fill=TRUE) > a ABC 1 1 2 3 2 4 5 NA 3 6 7 8 

You use standard configuration commands to remove (if you want) any lines containing NA :

 > a[!is.na(a$C),] ABC 1 1 2 3 3 6 7 8 
+12
source share

All Articles