Importing “csv” file with multiple-character separator to R?

后端 未结 2 1573
悲哀的现实
悲哀的现实 2020-12-03 16:15

I have a \"csv\" text file where each field is separated by \\t&%$# which I\'m now trying to import into R.

The sep= argument of

2条回答
  •  一个人的身影
    2020-12-03 16:56

    The following code will be able to handle multiple separator chars:

    #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))
    }
    

提交回复
热议问题