I have a large data file consisting of a single line of text. The format resembles
Cat 14 Dog 15 Horse 16
I\'d eventually like to get
Assuming that the white space is a delimiter, you can use the following mechanism:
scan
to read the filematrix
, then to a data.frame
The code:
x <- scan(file=textConnection("
Cat 14 Dog 15 Horse 16
"), what="character")
xx <- as.data.frame(matrix(x, ncol=2, byrow=TRUE))
names(xx) <- c("Animal", "Number")
xx$Number <- as.numeric(xx$Number)
The results:
xx
Animal Number
1 Cat 1
2 Dog 2
3 Horse 3