I a have a text file where every line is like that: "2007-12-03 13:50:17 Juan Perez" ("yy-mm-dd hh:mm:ss First Name Second Name") I would like to make a data frame with two column one for date and the other one for name. When I use read.delim it was transformed in a data frame with 4 colums. Bye, Sebasti?n.
You data has 4 fields (separated by blanks) and that is what you are reading. Just write some code to combine the fields: newDF <- data.frame(time=as.POSIXct(paste(oldDF[[1]], oldDF[[2]]), name=paste(oldDF[[3]], oldDF[[4]])) On Fri, Jun 18, 2010 at 10:58 AM, Sebastian Kruk <residuo.solow at gmail.com> wrote:> I a have a text file where every line is like that: > > "2007-12-03 13:50:17 Juan Perez" > ("yy-mm-dd hh:mm:ss First Name Second Name") > > I would like to make a data frame with two column one for date and the > other one for name. > > When I use read.delim it was transformed in a data frame with 4 colums. > > Bye, > > Sebasti?n. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
As the footer says:> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.There's probably another way using readLines or so to do it in one try, but say you used : frame <- read.delim("some_file.ext") then library(chron) newframe$name <- paste(frame[,3],frame[,4]) newframe$dates <- chron(frame[,1],frame[,2],format=c(dates = "y/m/d", times = "h:m:s")) Cheers Joris On Fri, Jun 18, 2010 at 4:58 PM, Sebastian Kruk <residuo.solow at gmail.com> wrote:> I a have a text file where every line is like that: > > "2007-12-03 13:50:17 Juan Perez" > ("yy-mm-dd hh:mm:ss First Name Second Name") > > I would like to make a data frame with two column one for date and the > other one for name. > > When I use read.delim it was transformed in a data frame with 4 colums. > > Bye, > > Sebasti?n. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Joris Meys Statistical consultant Ghent University Faculty of Bioscience Engineering Department of Applied mathematics, biometrics and process control tel : +32 9 264 59 87 Joris.Meys at Ugent.be ------------------------------- Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php
Try this: Lines <- "2007-12-03 13:50:17 Juan Perez" read.csv2(textConnection(gsub("(:\\d{2})\\s", "\\1;", Lines)), header FALSE) On Fri, Jun 18, 2010 at 11:58 AM, Sebastian Kruk <residuo.solow@gmail.com>wrote:> I a have a text file where every line is like that: > > "2007-12-03 13:50:17 Juan Perez" > ("yy-mm-dd hh:mm:ss First Name Second Name") > > I would like to make a data frame with two column one for date and the > other one for name. > > When I use read.delim it was transformed in a data frame with 4 colums. > > Bye, > > Sebastián. > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
On Fri, Jun 18, 2010 at 10:58 AM, Sebastian Kruk <residuo.solow at gmail.com> wrote:> I a have a text file where every line is like that: > > "2007-12-03 13:50:17 Juan Perez" > ("yy-mm-dd hh:mm:ss First Name Second Name") > > I would like to make a data frame with two column one for date and the > other one for name.Suppose this is your data: Lines <- "2007-12-03 13:50:17 Juan Perez 2008-12-03 13:50:17 Juanita Perez" # 1. sub # This reads it into data frame out L <- readLines(textConnection(Lines)) L <- sub(" ", ",", L) L <- sub(" ", ",", L) out <- read.table(textConnection(L), sep = ",", as.is = TRUE) # 1a. strapply # An alternate way to do this is with strapply in the gsubfn package library(gsubfn) L <- readLines(textConnection(Lines)) out <- strapply(L, "(\\S+) (\\S+) (.*)", c, simplify = rbind) # we convert 1 or 1a to data frame with chron dates and times library(chron) data.frame(date = dates(as.chron(out[,1])), time = times(out[,2]), value = out[,3], stringsAsFactors = FALSE) # 2. timedate # actually the above may not be the best representation. In almost all cases you # want to keep the date/time together and only extract the portion # you want at the last minute during processing. # sub L <- readLines(textConnection(Lines)) L <- sub(" ", ",", L) L <- sub(" ", ",", L) L <- sub(",", " ", L) out <- read.table(textConnection(L), sep = ",", as.is = TRUE) library(chron) data.frame(datetime = as.chron(out[,1]), value = out[,2], stringsAsFactors = FALSE) # 3. time series # Also is this a time series? If it is then you would be better off # representing it as such. L <- readLines(textConnection(Lines)) L <- sub(" ", ",", L) L <- sub(" ", ",", L) L <- sub(",", " ", L) library(zoo) library(chron) read.zoo(textConnection(L), sep = ",", FUN = as.chron)
Thanks, yes, I would like to do it in one try. I a have a text file called archivo where every line is like that: "2007-12-03 13:50:17 Juan Perez" ("yy-mm-dd hh:mm:ss First Name Second Name") My code is it: datos <- read.delim(archivo,header=FALSE,sep= " ",dec=".", col.names=c("date","time","fname", "sname",comment="#", colClasses = c("character","character","numeric","numeric")) f_h <- paste(datos[,"date"],datos[,"time"],sep=" ") fecha_hora <- strptime(f_h, "%y-%m-%d %H:%M:%S") Bye, Sebasti?n.