当列数变化时导入txt文件? [英] Importing a txt file when number of columns varies?
问题描述
我在将 .txt
文件导入 R 时遇到问题,因为数字列从八变为九.最初,我的数据有八列:
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
稍后,我添加了一个附加列Unadjusted close
.我应该如何导入数据?不知何故,必须在开始时忽略 Unadjusted close
列.我试过了
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)
但这不起作用.
推荐答案
您需要在 read.table
函数中使用 fill
参数.假设我有以下文件
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
称为tmp.txt
.请注意,第二行只有两个值.然后
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
您可以使用标准的子设置命令来删除(如果您愿意)任何包含 NA
的行:
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
这篇关于当列数变化时导入txt文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!