将 comment() 分配给 .txt 文件中的 R 对象
Assigning `comment()` to an R object from a .txt file
我正在尝试将评论分配给数据框以存储一些相关的元数据。我有一个用引号引起来的非结构化文本文件,其中有几个换行符 ('\n')。
WHO_comment<-read.table(file="WHO comment.txt", sep="\t")
comment(WHO)<-WHO_comment #Read in the comment from .txt due to its length
cat(comment(WHO)) #Database metadata
但是,读数出现在一个大块中,其中“\n”读作文字字符串。仅使用 as.character()
转换它 returns 行名(即“1”)。
如何正确读取此文件?
已解决 - 我需要使用 stringsAsFactors=FALSE
才能正确读取文件。这段代码现在可以完成我想要的功能,即从 .txt 文件分配注释。
WHO_comment<-read.table(file="WHO comment.txt", sep="\t",stringsAsFactors=FALSE)
comment(WHO)<-WHO_comment #Read in the comment from .txt due to its length
cat(comment(WHO)) #Database metadata
read.table
是读取文本文件的错误函数。顾名思义,它的用途是读取表格数据。要读取文本文件,请使用 readLines
,然后将各行粘贴在一起:
comment(data) = paste(readLines('WHO comment.txt'), collapse = '\n')
我正在尝试将评论分配给数据框以存储一些相关的元数据。我有一个用引号引起来的非结构化文本文件,其中有几个换行符 ('\n')。
WHO_comment<-read.table(file="WHO comment.txt", sep="\t")
comment(WHO)<-WHO_comment #Read in the comment from .txt due to its length
cat(comment(WHO)) #Database metadata
但是,读数出现在一个大块中,其中“\n”读作文字字符串。仅使用 as.character()
转换它 returns 行名(即“1”)。
如何正确读取此文件?
已解决 - 我需要使用 stringsAsFactors=FALSE
才能正确读取文件。这段代码现在可以完成我想要的功能,即从 .txt 文件分配注释。
WHO_comment<-read.table(file="WHO comment.txt", sep="\t",stringsAsFactors=FALSE)
comment(WHO)<-WHO_comment #Read in the comment from .txt due to its length
cat(comment(WHO)) #Database metadata
read.table
是读取文本文件的错误函数。顾名思义,它的用途是读取表格数据。要读取文本文件,请使用 readLines
,然后将各行粘贴在一起:
comment(data) = paste(readLines('WHO comment.txt'), collapse = '\n')