我有一个“csv”文本文件,其中每个字段都由\t&%$#
我现在尝试导入到 R 中分隔。
坚持单个字符的sep=
论点read.table()
。有没有直接导入这个文件的快速方法?
一些数据字段是用户提交的文本,其中包含制表符、引号和其他杂乱的内容,因此将分隔符更改为更简单的内容似乎会产生其他问题。
小智 6
以下代码将能够处理多个分隔符:
#fileName <- file name with fully qualified path
#separators <- each of them separated by '|'
read <- function(fileName, separators) {
data <- readLines(con <- file(fileName))
close(con)
records <- sapply(data, strsplit, split=separators)
dataFrame <- data.frame(t(sapply(records,c)))
rownames(dataFrame) <- 1: nrow(dataFrame)
return(as.data.frame(dataFrame,stringsAsFactors = FALSE))
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9915 次 |
最近记录: |