【问题标题】:R How do I create a list with text from multiple txt files?R 如何创建包含多个 txt 文件中的文本的列表?
【发布时间】:2018-11-08 12:05:15
【问题描述】:

我正在尝试通读 n 个 .txt 文件的列表并将内容保存到字符列表中。但是,当我阅读它们并将它们添加到列表“txts”中时,它给了我错误: "阅读 32 项 要替换的项目数不是替换长度的倍数"

每个 txt 文件包含 32 行,每行 32 个字符(“0”和“1”)。第一步,我想获得一个包含 txt 文件内容的 n 个元素列表(每个元素有 32 个项目,每个项目 32 个字符)。

然后我需要将每个 32x32 字符转换为 1x1024 向量(我将使用 strstrip),最后我需要有一个 n(txt 数)行和 1024 列的矩阵(每个 txt 文件的字符数)。

通过以下内容,我尝试创建两个 txt 文件内容的列表,但出现错误。我使用扫描是因为我需要 txt 文件中的所有字符。我尝试了 read.table ,它工作正常,但它会将它们转换为数字,我不希望这样。

file_example <-list("digits/trainingDigits/0_0.txt","digits/trainingDigits/0_1.txt")

txts <- c()

for (i in file_example){
  nb = scan(s,what="character", sep=NULL)
  txts[s] <- c(nb)
}

关于如何解决它的任何想法?我希望它足够清楚。

谢谢!!


更新:

我尝试了以下代码:

file_example <- c("digits/trainingDigits/0_0.txt","digits/trainingDigits/0_1.txt")

txts <- c()

mylist <- list()
for (i in 1: length(file_example)){
  nb = read.csv(file_example[[i]])
  txts[s] <- c(nb)
  txts <- as.character(nb$X)
  mylist[[i]] <- txts
}

但是,我得到的是一个包含两个元素的列表,并且在 32 个数字内,但我认为这些字符没有保留。

两个列表的第一个元素:

[[1]] [1] “1.111111e+19” “1.111111111e+21” “1.1111111111111e+24” “1.1111110111111e+24” “1.11111100000111e+25” [6] "1.11111100000001e+25" "1.11111100000001e+25" "1.111111e+25" "1.111111e+25" "1.11111e+25"
[11] "1.11111e+25" "1.11111e+25" "1.11111e+24" "1.11111e+24" "1.11111e+24"
[16] “1.11111e+24” “1.111111e+25” “1.111011e+25” “1.111e+25” “1.111e+24”
[21] “1.11100000000001e+24” “1.11100000000011e+24” “1.11100000000111e+24” “1.11000000111111e+23” “1.1110001111111e+23” [26] “1.1111111111111e+23” “1.111111111111e+22” “1.11111111111e+22” “1.11111111e+21” “1.1111e+20”
[31]“1.1e+19”

最终目标是为每个 .txt 文件获取此信息,以便将其转换为具有 1024 个元素的向量:

"[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 [64] 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 [127] 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 [190] 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 [253] 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 0 0 0 [316] 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 0 [379] 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 0"

这将有 1024 个字符

谢谢

【问题讨论】:

  • 我们可以在read.table中设置colClasses=来避免将输入转换为数字。
  • 请提供示例文本文件。
  • 你能试试txts &lt;- lapply(file_example, scan, what="character", sep=NULL)之类的吗?

标签: r list file loops path


【解决方案1】:

据我了解,您的问题是将每个文档存储为一个列表,并将数值作为字符。事实上,您可以使用as.charachter() 将它们转换回字符 这里我提供一个简单的例子。如果您的数据有更多列,只需将向量 txts 更改为矩阵并放置一个额外的 for 循环来存储它对每一行的索引。

######### Creating the files
a<- c("0", "2","1","1")
b<- c("0","1","1","0")

#### path to read from and store the example
path<-"C:/Users/Carles/Desktop/"

write.csv(a,paste0(path,"a.txt"))
write.csv(b,paste0(path,"b.txt"))

# reading example
file_example<- c(paste0(path,"a.txt"), paste0(path,"b.txt"))

# creating a list to store values
mylist<-list()
for (i in 1:length(file_example)){
  nb = read.csv(file_example[i]) # reading the documents
  txts <- as.character(nb$x) # getting the vector x since read.csv stores data in dataset form
  mylist[[i]]<-txts # passing the texts stored to the list
}



> mylist
[[1]]
[1] 0 2 1 1

[[2]]
[1] 0 1 1 0

希望对你有帮助! BR,

PD:下次请举个例子。这对于更好地理解问题以及能够重现问题很重要。

【讨论】:

  • 嗨 Carles 感谢您的建议和回复,这是我第二次来这里了。只是为了提供更多信息,打开 0001 0110 0000 1110 时的 txt 看起来像这样,但数据量更大(32x32 个字符)。使用您的代码的输出是: [[1]] [1] "1.111111e+19" "1.111111111e+21" "1.1111111111111e+24" "1.1111110111111e.... 最终目标是得到这个:[ 1] 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0...
  • 你需要提供你的部分清单让我理解
猜你喜欢
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
  • 2021-09-21
  • 1970-01-01
  • 2017-02-17
  • 1970-01-01
  • 2016-06-27
相关资源
最近更新 更多