删除每个.txt文件的最后一行并导入/合并R中的数据

时间:2015-03-23 17:17:32

标签: r import merge dataframe

我是R的新手,似乎无法绕过这个。我在文件夹中有多个.txt文件,并希望导入和合并数据。我的问题是每个文本文件的最后一行。我尝试了多种方法,但无法找到解决方案。预先感谢您的任何帮助。如果有任何问题,请告诉我以下内容。

文本文件1

|03/09/2015|Fist Name 1 |Last Name 1|Address 1
|03/09/2015|First Name 2|Last Name 2|Address 2
|2|2|||||||||||

文本文件2

|03/06/2015|First Name 3|Last Name 3|Address 3
|03/06/2015|First Name 4|Last Name 4|Address 4
|03/06/2015|First Name 5|Last Name 5|Address 5
|3|3|||||||||||

寻找以下结果

V1                   V2                 V3                 V4
03/09/2015      First Name 1          Last Name 1        Address 1
03/09/2015      First Name 2          Last Name 2        Address 2
03/06/2015      First Name 3          Last Name 3        Address 3
03/06/2015      First Name 4          Last Name 4        Address 4
03/06/2015      First Name 5          Last Name 5        Address 5

1 个答案:

答案 0 :(得分:3)

您可以使用count.fields()帮助计算nrowsread.table()的数量。您必须删除第一列,因为它将为空

files <- c("x1.txt", "x2.txt")

inList <- lapply(files, function(x) {
    read.table(x, nrows = length(count.fields(x))-1, sep = "|")[-1]
})
do.call(rbind, inList)
#           V2           V3          V4        V5
# 1 03/09/2015 Fist Name 1  Last Name 1 Address 1
# 2 03/09/2015 First Name 2 Last Name 2 Address 2
# 3 03/06/2015 First Name 3 Last Name 3 Address 3
# 4 03/06/2015 First Name 4 Last Name 4 Address 4
# 5 03/06/2015 First Name 5 Last Name 5 Address 5

你可以添加

colClasses = c("NULL", "character", "character", "NULL", "character"))

上述read.table()调用仅选择第2,3和5列(请参阅下面的评论)


另一种方法是使用fread()。它使得选择列更容易。

library(data.table)

rbindlist(
    lapply(files, function(x) {
        nr <- length(count.fields(x))-1
        suppressWarnings(
            fread(x, nrows = nr, sep = "|", header = FALSE, drop = c(1, 4))
        )
    })
)
#            V2           V3        V5
# 1: 03/09/2015 Fist Name 1  Address 1
# 2: 03/09/2015 First Name 2 Address 2
# 3: 03/06/2015 First Name 3 Address 3
# 4: 03/06/2015 First Name 4 Address 4
# 5: 03/06/2015 First Name 5 Address 5

注意:这是使用data.table development version 1.9.5


x1.txtx2.txt

的位置
writeLines("|03/09/2015|Fist Name 1 |Last Name 1|Address 1
|03/09/2015|First Name 2|Last Name 2|Address 2
|2|2|||||||||||", "x1.txt")

writeLines("|03/06/2015|First Name 3|Last Name 3|Address 3
|03/06/2015|First Name 4|Last Name 4|Address 4
|03/06/2015|First Name 5|Last Name 5|Address 5
|3|3|||||||||||", "x2.txt")