R从文本文件导入后修剪字符串

时间:2018-07-10 21:38:08

标签: r import trim

.txt中的每一行都包含一个文本,例如:

FORMAT=2.1
(TYPE=test
[DESCRIPTION="This is a test data set for study purpose only"
]
)

但是,将其导入R后,某些字符没有完全导入。数据类型为tbl_df

示例: .txt中的字符串:

[DESCRIPTION="This is a test data set for study purpose only"

我希望导入的字符串为:

"[DESCRIPTION=\"This is a test data set for study purpose only\"" 

但是,这是Rstudio中出现的字符串,

"[DESCRIPTION=\"This is a test data set for study purpose "

是因为string类型的数据有限制吗?我使用nchar(),看到所有字符串在第56个字符之后都消失了。

是否可以将所有内容保持在一行中?谢谢您的帮助。

只要意识到这是我的错误。文本文件无法导入:

这是文本格式和错误

FORMAT=2.1
(TYPE=Line2
[DESCRIPTION="This is a test data set for study purpose only"
]
)
(TYPE=Line6
[DESCRIPTION="This is a test data set for study purpose only"
]
)
(TYPE=Line10
[DESCRIPTION="This is a test data set for study purpose only"
]
)

> rtest <- read.table("C:/Users/Desktop/rtest.txt", quote="\"", comment.char="")
Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec,  : 
  line 1 did not have 10 elements

因为我首先使用excel导入,所以我认为文本文件已导入,但实际上我在看excel数据集。因此,这将是一个不同的问题,谢谢大家的帮助。与你们更新后,我将立即删除此帖子。

0 个答案:

没有答案