2016-10-18 49 views
-1

我在我的目录900个的文本文件,如下[R脚本文件

enter image description here

每个文件包含的数据按以下格式在下面的图中可以看出

667869 667869.000000 
580083 580083.000000 
316133 316133.000000 
11065 11065.000000 

我想从每个文本文件中提取第四行并将值存储在数组中,欢迎任何建议

回答

0

这听起来更像一个StackOverflow的问题,类似于 Importing multiple .csv files into R

你可以尝试这样的:

setwd( “/路径/到/文件”)

文件< - list.files(PATH = getwd( ),递归= FALSE)

头(文件)

myfiles的= lapply(文件,函数(X)read.csv(文件= X,标题= TRUE))

MYDATA = lapply(myfiles的,FUN =函数(DF){DF [4,]}) STR(MYDATA)

do.call(rbind,MYDATA)

0

懒答案是:

array <- c() 
for (file in dir()) { 
    row4 <- read.table(file, 
        header = FALSE, 
        row.names = NULL, 
        skip = 3, # Skip the 1st 3 rows 
        nrows = 1, # Read only the next row after skipping the 1st 3 rows 
        sep = "\t") # change the separator if it is not "\t" 
    array <- cbind(array, row4) 
} 

可以进一步保持文件的名称

colnames(array) <- dir() 
+0

我想使用模式regional_vol _ *。txt读取多个文件,所以当我使用文件名regional_vol _ *。txt时,它抛出一个错误 – DevanDevak