I'm a great fan of read.table(), but this time the data had a lot of cruft. So I used readLines() and editted the char vector to eventually get something like this: " 23.4 1.5 4.2" " 19.1 2.2 4.1" and so on. To get that into a 3 col numeric table, I first just used: writeLines(data,"tempfile") read.table("tempfile",col.names=c("A","B","C")) Works fine, but writing to a temporary file seems ... inelegant? And read.table() doesn't take a char vector as a file or connection argument. The following works but it seems like a lot of code: data <- sub(" +","",data) # remove leading blanks for strsplit data <- strsplit(data," +") # strsplit returns a list of char vectors ndata <- character(0) # vectorize the list of char vectors for (ii in 1:length(data)) ndata <- c(ndata,data[[ii]]) ndata <- as.numeric(ndata) dim(ndata) <- c(3,length(data)) data <- t(ndata) data.frame(A=data[,1],B=data[,2],C=data[,3]) Am I missing something? Thanks, Bruce L.
Nurnberg-LaZerte wrote:> I'm a great fan of read.table(), but this time the data had a lot of cruft. So I used readLines() and editted the char vector to eventually get something like this: > " 23.4 1.5 4.2" > " 19.1 2.2 4.1" > and so on. To get that into a 3 col numeric table, I first just used: > > writeLines(data,"tempfile") > read.table("tempfile",col.names=c("A","B","C")) > > Works fine, but writing to a temporary file seems ... inelegant? And read.table() doesn't take a char vector as a file or connection argument. The following works but it seems like a lot of code: > > data <- sub(" +","",data) # remove leading blanks for strsplit > data <- strsplit(data," +") # strsplit returns a list of char vectors > ndata <- character(0) # vectorize the list of char vectors > for (ii in 1:length(data)) ndata <- c(ndata,data[[ii]]) > ndata <- as.numeric(ndata) > dim(ndata) <- c(3,length(data)) > data <- t(ndata) > data.frame(A=data[,1],B=data[,2],C=data[,3]) >Bruce, I don't know if this is any more elegant, but... data <- c(" 23.4 1.5 4.2"," 19.1 2.2 4.1") data.new <- as.data.frame(t(sapply(strsplit(gsub("[ \t]*"," ",data)," "),as.numeric))[,-1]) names(data.new) <- LETTERS[1:3] Sundar
Have you considered something like the following: > tst <- c(" 23.4 1.5 4.2", " 19.1 2.2 4.1") > num1 <- regexpr("[0-9]", tst) > tst1 <- substring(tst, num1) > b1 <- regexpr(" ", tst1) > x1 <- as.numeric(substring(tst1, 1, num1)) > tst <- substring(tst1, b1) > x1; tst [1] 23.4 19.1 [1] " 1.5 4.2" " 2.2 4.1" You can put this in a loop with appropriate modification for the end of the strings, etc. Will this solve your problems? Best Wishes, Spencer Graves Nurnberg-LaZerte wrote:> I'm a great fan of read.table(), but this time the data had a lot of cruft. So I used readLines() and editted the char vector to eventually get something like this: > " 23.4 1.5 4.2" > " 19.1 2.2 4.1" > and so on. To get that into a 3 col numeric table, I first just used: > > writeLines(data,"tempfile") > read.table("tempfile",col.names=c("A","B","C")) > > Works fine, but writing to a temporary file seems ... inelegant? And read.table() doesn't take a char vector as a file or connection argument. The following works but it seems like a lot of code: > > data <- sub(" +","",data) # remove leading blanks for strsplit > data <- strsplit(data," +") # strsplit returns a list of char vectors > ndata <- character(0) # vectorize the list of char vectors > for (ii in 1:length(data)) ndata <- c(ndata,data[[ii]]) > ndata <- as.numeric(ndata) > dim(ndata) <- c(3,length(data)) > data <- t(ndata) > data.frame(A=data[,1],B=data[,2],C=data[,3]) > > Am I missing something? > > Thanks, > Bruce L. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
james.holtman@convergys.com
2003-Apr-01 00:22 UTC
[R] Convert char vector to numeric table
use 'textConnection':> x.1 <- c('1 2 3','4 5 6','7 8 9','8 7 6','6 5 4') # create charactervector> x.in <- textConnection(x.1) # setup connection > x.data <- read.table(x.in) # read in the character vector > x.dataV1 V2 V3 1 1 2 3 2 4 5 6 3 7 8 9 4 8 7 6 5 6 5 4>"Nurnberg-LaZerte" <mail at fwr.on.ca> To: "R's help mailing list" <r-help at stat.math.ethz.ch> Sent by: cc: r-help-bounces at stat.m Subject: [R] Convert char vector to numeric table ath.ethz.ch 03/31/03 17:09 Please respond to Nurnberg-LaZerte I'm a great fan of read.table(), but this time the data had a lot of cruft. So I used readLines() and editted the char vector to eventually get something like this: " 23.4 1.5 4.2" " 19.1 2.2 4.1" and so on. To get that into a 3 col numeric table, I first just used: writeLines(data,"tempfile") read.table("tempfile",col.names=c("A","B","C")) Works fine, but writing to a temporary file seems ... inelegant? And read.table() doesn't take a char vector as a file or connection argument. The following works but it seems like a lot of code: data <- sub(" +","",data) # remove leading blanks for strsplit data <- strsplit(data," +") # strsplit returns a list of char vectors ndata <- character(0) # vectorize the list of char vectors for (ii in 1:length(data)) ndata <- c(ndata,data[[ii]]) ndata <- as.numeric(ndata) dim(ndata) <- c(3,length(data)) data <- t(ndata) data.frame(A=data[,1],B=data[,2],C=data[,3]) Am I missing something? Thanks, Bruce L. ______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help -- "NOTICE: The information contained in this electronic mail transmission is intended by Convergys Corporation for the use of the named individual or entity to which it is directed and may contain information that is privileged or otherwise confidential. If you have received this electronic mail transmission in error, please delete it from your system without copying or forwarding it, and notify the sender of the error by reply email or by telephone (collect), so that the sender's address records can be corrected."
Use a file() connection (no argument) or a textConnection. file() is a powerful tool, often overlooked. On Mon, 31 Mar 2003, Nurnberg-LaZerte wrote:> I'm a great fan of read.table(), but this time the data had a lot of cruft. So I used readLines() and editted the char vector to eventually get something like this: > " 23.4 1.5 4.2" > " 19.1 2.2 4.1" > and so on. To get that into a 3 col numeric table, I first just used: > > writeLines(data,"tempfile") > read.table("tempfile",col.names=c("A","B","C")) > > Works fine, but writing to a temporary file seems ... inelegant? And read.table() doesn't take a char vector as a file or connection argument. The following works but it seems like a lot of code: > > data <- sub(" +","",data) # remove leading blanks for strsplit > data <- strsplit(data," +") # strsplit returns a list of char vectors > ndata <- character(0) # vectorize the list of char vectors > for (ii in 1:length(data)) ndata <- c(ndata,data[[ii]]) > ndata <- as.numeric(ndata) > dim(ndata) <- c(3,length(data)) > data <- t(ndata) > data.frame(A=data[,1],B=data[,2],C=data[,3])-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595