[R] split character line into rows

David L Carlson dcarlson at tamu.edu
Mon Dec 17 22:48:08 CET 2012


To combine the date/time fields into one and
rename the variables, use this:

> dta
          V1       V2     V3    V4 V5
1 2010.12.26 00:00:52 688.88 11.69 43
2 2010.12.26 00:01:52 696.19 11.69 43

> dta <- data.frame(date=strptime(paste(dta$V1, 
  dta$V2), "%Y.%m.%d %H:%M:%S"), V2=dta$V3, 
  V3=dta$V4, V4=dta$V5)

> dta
                 date     V2    V3 V4
1 2010-12-26 00:00:52 688.88 11.69 43
2 2010-12-26 00:01:52 696.19 11.69 43

> str(dta)
'data.frame':   2 obs. of  4 variables:
 $ date: POSIXct, format: "2010-12-26 00:00:52" 
     "2010-12-26 00:01:52"
 $ V2  : num  689 696
 $ V3  : num  11.7 11.7
 $ V4  : num  43 43

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of arun
> Sent: Monday, December 17, 2012 2:35 PM
> To: Simonas Kecorius
> Cc: R help
> Subject: Re: [R] split character line into rows
> 
> Hi,
> This could also work:
>  max(nchar(txt))
> #[1] 58
> res<-
> read.table(text=substr(txt[nchar(txt)>20],5,58),sep="",dec=",",header=F
> ALSE,stringsAsFactors=FALSE)
>  res
> #          V1       V2     V3    V4 V5
> #1 2010.12.26 00:00:52 688.88 11.69 43
> #2 2010.12.26 00:01:52 696.19 11.69 43
> A.K.
> 
> 
> 
> 
> ----- Original Message -----
> From: David Winsemius <dwinsemius at comcast.net>
> To: Simonas Kecorius <simolas2008 at gmail.com>
> Cc: r-help at r-project.org
> Sent: Monday, December 17, 2012 3:15 PM
> Subject: Re: [R] split character line into rows
> 
> 
> On Dec 17, 2012, at 4:04 AM, Simonas Kecorius wrote:
> 
> > Hey R users,
> >
> > suppose we have data:
> >
> txt <- readLines(textConnection("[1] 2010.12.26 00:00:52    688,88
>     11,69     43,00
> [2] 11,69     43,00
> [3] 11,69     43,00
> [4] 11,69     43,00
> [5] 11,69     43,00
> [6] 11,69     43,00
> [7] 11,69     43,00
> [8] 11,69     43,00
> [9] 11,69     43,00
> [10] 11,69     43,00
> [11] 11,69     43,00
> [12] 11,69     43,00
> [13] 11,69     43,00
> [14] 11,69     43,00
> [15] 11,69     43,00
> [16] 11,69     43,00
> [17] 11,69     43,00
> [18] 11,69     43,00
> [19] 11,69     43,00
> [20] 11,69     43,00
> [21] 11,69     43,00
> [22] 11,69     43,00
> [23] 11,69     43,00
> [24] 11,69     43,00
> [25] 11,69     43,00
> [26] 11,69     43,00
> [27] 11,69     43,00
> [28] 11,69     43,00
> [29] 11,69     43,00
> [30] 11,69     43,00
> [31] 11,69     43,00
> [32] 11,69     43,00
> [33] 11,69     43,00
> [34] 11,69     43,00
> [35] 11,69     43,00
> [36] 11,69     43,00
> [37] 11,69     43,00
> [38] 11,69     43,00
> [39] 11,69     43,00
> [40] 11,69     43,00
> [41] 11,69     43,00
> [42] 11,69     43,00
> [43] 11,69     43,00
> [44] 11,69     43,00
> [45] 11,69     43,00
> [46] 11,69     43,00
> [47] 11,69     43,00
> [48] 11,69     43,00
> [49] 11,69     43,00
> [50] 11,69     43,00
> [51] 11,69     43,00
> [52] 11,69     43,00
> [53] 11,69     43,00
> [54] 11,69     43,00
> [55] 11,69     43,00
> [56] 11,69     43,00
> [57] 11,69     43,00
> [58] 11,69     43,00
> [59] 11,69     43,00
> [60] 11,69     43,00
> [61] 2010.12.26 00:01:52    696,19          11,69     43,00
> [62] 11,69     43,00
> [63] 11,69     43,00
> [64] 11,69     43,00
> [65] 11,69     43,00
> [66] 11,69     43,00"))
> 
> txt <- sub("\\[.+\\]","", txt)
> read.table(text=txt[ grepl("[[:digit:]]{4}\\.", txt) ] )
>           V1       V2     V3    V4    V5
> 1 2010.12.26 00:00:52 688,88 11,69 43,00
> 2 2010.12.26 00:01:52 696,19 11,69 43,00
> 
> Since you seemed to be using commas for decimal points I thought search
> for "NNNN." as a pattern might be sufficient, but you could extend that
> to a full date matching pattern if needed.
> 
> 
> > ..................................... etc.
> >
> > Is there a way to split data into date column, V2, V3 and V4 columns
> and
> > erase those lines without date, so that data would look like that:
> >
> > date                             V2       V3       V4
> > 2010.12.26 00:01:52     555     11.67     44
> > 2010.12.26 00:02:52     566     11.67     44
> >
> > etc.
> >
> > Thanks a lot!
> >
> >
> > --
> > Simonas Kecorius
> > **
> >
> >     [[alternative HTML version deleted]]
> 
> Please read the Posting Guide and learn to post in plain text.
> 
> 
> David Winsemius
> Alameda, CA, USA
> 
> ______________________________________________
> 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.
> 
> 
> ______________________________________________
> 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.




More information about the R-help mailing list