Having troubles reading a txt file into R with columns delimited by ||

后端 未结 1 1821
故里飘歌
故里飘歌 2021-01-15 01:25

I\'m having troubles trying to read a .txt file consisting of 561366 rows and 15 columns. The first rows look something like this:

  70000||Consumer A||23||D         


        
相关标签:
1条回答
  • 2021-01-15 01:38

    Methinks the sep= character has to be of length one. So you could simply call either an external command (like sed or perl) to substitute the two-character ||, or do it yourself in R.

    Here is a proof of concept one-liner that first switches the || to , (and as | is a meta character we need to escape it, and escape the escape character \ too):

    R> read.csv(textConnection(gsub("\\|\\|", ",", "70000||Consumer A||23||DN||70000||10038782||1||SI||2||NO||0||N/A||0||N/A||1\n90000||Consumer B||23||DN||90000||15402432||1||SI||2||NO||0||N/A||0||N/A||1")), header=FALSE)
         V1         V2 V3 V4    V5       V6 V7 V8 V9 V10 V11 V12 V13 V14 V15
    1 70000 Consumer A 23 DN 70000 10038782  1 SI  2  NO   0 N/A   0 N/A   1
    2 90000 Consumer B 23 DN 90000 15402432  1 SI  2  NO   0 N/A   0 N/A   1
    R> 
    

    or maybe more legible in two lines:

    R> txt <- "70000||Consumer A||23||DN||70000||10038782||1||SI||2||NO||0||N/A||0||N/A||1\n90000||Consumer B||23||DN||90000||15402432||1||SI||2||NO||0||N/A||0||N/A||1"
    R> read.csv(textConnection(gsub("\\|\\|", ",", txt)), header=FALSE)
         V1         V2 V3 V4    V5       V6 V7 V8 V9 V10 V11 V12 V13 V14 V15
    1 70000 Consumer A 23 DN 70000 10038782  1 SI  2  NO   0 N/A   0 N/A   1
    2 90000 Consumer B 23 DN 90000 15402432  1 SI  2  NO   0 N/A   0 N/A   1
    R> 
    
    0 讨论(0)
提交回复
热议问题