How to read a text file into GNU R with a multiple-byte separator?

后端 未结 2 1991
遥遥无期
遥遥无期 2020-12-06 18:37

I can use read.csv or read.csv2 to read data into R. But the issue I encountered is that my separator is a multiple-byte string instead of a single character. How can I deal

2条回答
  •  没有蜡笔的小新
    2020-12-06 18:54

    In this case you can replace textConnection(txt) with your file name, but essentially you can build a code or function around strsplit. Here I'm assuming you have a header line, but you can of course give define a header argument and generalize the creation of your data frame based on the function below:

    read.multisep <- function(File,sep) {
        Lines <- readLines(File)
        Matrix <- do.call(rbind,strsplit(Lines, sep, fixed = TRUE))
        DataFrame <- structure(data.frame(Matrix[-1,]), names=Matrix[1,]) ## assuming header is present
        DataFrame[] <- lapply(DataFrame, type.convert)                    ## automatically convert modes
        DataFrame
    }
    
    example <- "a#*&b#*&c
                1#*&2#*&3
                4#*&5#*&6"
    
    read.multisep(textConnection(example),sep="#*&")
    
      a b c
    1 1 2 3
    2 4 5 6
    

提交回复
热议问题