简体   繁体   中英

read.table - long text split into different columns

I am trying to import the below data using the code below, the output I get has the long text split into different columns.

postcode <- read.table(file="workbook16.txt", header = T, fill = T)


Seq Suburb  Postcode
1   Melbourne   3000
2   Melbourne   3001
3   East Melbourne  3002
4   West Melbourne  3003
5   Melbourne   3004
6   World Trade Centre  3005
7   Southbank   3006
8   Docklands   3008
9   University Of Melbourne 3010

Where is my error?

One way would be to read using readLines

lines <- readLines(n=10)
Seq Suburb  Postcode
1   Melbourne   3000
2   Melbourne   3001
3   East Melbourne  3002
4   West Melbourne  3003
5   Melbourne   3004
6   World Trade Centre  3005
7   Southbank   3006
8   Docklands   3008
9   University Of Melbourne 3010

lines1 <- lines[-1]
 dat <- as.data.frame(do.call(rbind,strsplit(lines1, '(?<=[0-9])\\s+|\\s+(?=[0-9])', perl=T)))
colnames(dat) <- lines[1]
colnames(dat) <- scan(text=lines[1],what="")
dat[,c(1,3)] <- lapply(dat[,c(1,3)], function(x) as.numeric(as.character(x)))

dat
#  Seq                  Suburb Postcode
#1   1               Melbourne     3000
#2   2               Melbourne     3001
#3   3          East Melbourne     3002
#4   4          West Melbourne     3003
#5   5               Melbourne     3004
#6   6      World Trade Centre     3005
#7   7               Southbank     3006
#8   8               Docklands     3008
#9   9 University Of Melbourne     3010

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM