如何读取R中的zip文件并遍历每个.txt文件将其转换为.cvs?

时间:2015-08-13 18:12:29

标签: r csv

我有一个包含getGlobalVisibleRect个文档的zip文件。我想在R中解压缩文件并将文本文档转换为.txt,以便我可以将其用于进一步分析。

我可以在转换时提供标题名称吗?

此外,我还想通过编写一个函数来读取每个上面转换的.csv文件并从数据生成基本图形来迭代该过程。在R中这样做是否可行?

例如,让我们将zip文件名称视为'data.zip',其中包含5个文本文件(1.txt,2.txt,3.txt,4.txt,5.txt)。每个文本文件都有一行包含IP,日期和时间的日志信息。

.csv

你的答案将会有很大的帮助。 提前谢谢!

1 个答案:

答案 0 :(得分:1)

我创建了一个可重复的样本。

并认为这可以解决您的问题。

您可以下载我从here创建的示例zip文件。

附件是完整的代码。

## Clean Memory
rm(list=ls())

## Set path for your working location
setwd("D:/blah")

## unzipped it the file
unzip("D:/blah/text.zip")

## Check file in the zipped file
list.files()

## Read the file
temp = list.files(pattern="*.txt")

这里有选择。我想你想要的是第二个将样本中的两个文件组合在一起并将它们合并为一个文件。

## Read the file as list
myfiles= lapply(temp, read.delim)
## Read the file all together
myfiles = do.call("rbind", lapply(temp, function(x) read.table(x, stringsAsFactors = FALSE,header = TRUE)))

如果需要,请务必调整标题设置。 好的,祝你好运。