Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing a txt file when number of columns varies?

Tags:

r

text-files

I have problems importing a .txt file into R because the number columns changes from eight to nine. Initially, my data has eight columns:

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

Later, I add an additional column Unadjusted close. How should I import the data? Somehow the Unadjusted close column has to be ignored at the beginning. I've tried

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

but that doesn't work.

like image 462
Marko Avatar asked Mar 23 '11 08:03

Marko


People also ask

How do I copy a single text file into another column?

Press and hold the “Shift” and “Alt” keys on your keyboard. Continue holding “Shift” and “Alt” while using the “Down” and “Right” arrow keys on your keyboard to select the text as desired.


1 Answers

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 row two has only two values. Then

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

You use then standard sub-setting commands to remove (if you want to), any rows that contain NA:

> a[!is.na(a$C),]
  A B  C 
1 1 2  3
3 6 7  8
like image 175
csgillespie Avatar answered Nov 04 '22 19:11

csgillespie