Re: [R] Data separated by spaces, getting data into R using field lengths

2009-09-08 Thread David Winsemius
On Sep 8, 2009, at 12:00 PM, Lauri Nikkinen wrote: Ok, I think that I have to give up and try to get this data separated by some char. It seem pretty much impossible to separate those fields. Thanks for your help and efforts. The solution that Henrique offered seems to be a complete one: rea

Re: [R] Data separated by spaces, getting data into R using field lengths

2009-09-08 Thread Lauri Nikkinen
Ok, I think that I have to give up and try to get this data separated by some char. It seem pretty much impossible to separate those fields. Thanks for your help and efforts. -L 2009/9/8 Lauri Nikkinen : > This is the file (see the attachment) that represents the problem I'm > facing with the ori

Re: [R] Data separated by spaces, getting data into R using field lengths

2009-09-08 Thread Lauri Nikkinen
This is the file (see the attachment) that represents the problem I'm facing with the original file. I'm looking for some generic way to solve this problem. Thank you for your time. -L 2009/9/8 Barry Rowlingson : > On Tue, Sep 8, 2009 at 1:52 PM, Lauri Nikkinen wrote: > >> But this is not the sol

Re: [R] Data separated by spaces, getting data into R using field lengths

2009-09-08 Thread Barry Rowlingson
On Tue, Sep 8, 2009 at 1:52 PM, Lauri Nikkinen wrote: > But this is not the solution I was looking for. Thanks. I think the only way you'll get the solution you are looking for is if you can let us have a copy of the original input file, or at least the first few lines - and not pasted into an e

Re: [R] Data separated by spaces, getting data into R using field lengths

2009-09-08 Thread Henrique Dallazuanna
Try this: read.table(textConnection(gsub("([0-9]+)", ";\\1;", x)), sep = ";") On Tue, Sep 8, 2009 at 9:52 AM, Lauri Nikkinen wrote: > Thanks Petr, I tried something like this > > > con <- file("C:temppi.txt", "r", blocking = FALSE) > > g <- readLines(con) > > close(con) > > > > sta <- c(1, 3, 5,

Re: [R] Data separated by spaces, getting data into R using field lengths

2009-09-08 Thread Lauri Nikkinen
Thanks Petr, I tried something like this > con <- file("C:temppi.txt", "r", blocking = FALSE) > g <- readLines(con) > close(con) > > sta <- c(1, 3, 5, 19) > sto <- c(2, 4, 18, 100) > do.call("rbind", lapply(g, function(x) substring(x, sta, sto))) [,1] [,2] [,3] [,4] [1,] "DF" "12"

Re: [R] Data separated by spaces, getting data into R using field lengths

2009-09-08 Thread Philipp Pagel
On Tue, Sep 08, 2009 at 03:21:53PM +0300, Lauri Nikkinen wrote: > This data is from database and the maximum length of a field is > defined. I mean that every column has a maximum length and I want to > use this maximum length as a separator. So if one "cell" in that > column is shorter than the ma

Re: [R] Data separated by spaces, getting data into R using field lengths

2009-09-08 Thread Petr PIKAL
Hi what about reading each line by readLine and then split it to desired portions? x<-paste(letters, collapse="") substring(x, c(1,3,5),c(2,4,15)) Regards Petr r-help-boun...@r-project.org napsal dne 08.09.2009 14:21:53: > This data is from database and the maximum length of a field is > def

Re: [R] Data separated by spaces, getting data into R using field lengths

2009-09-08 Thread Lauri Nikkinen
Thanks for the suggestion, but I don't have an access to this database, I just got this messy file. -L 2009/9/8 Duncan Murdoch : > On 9/8/2009 8:21 AM, Lauri Nikkinen wrote: >> >> This data is from database and the maximum length of a field is >> defined. I mean that every column has a maximum le

Re: [R] Data separated by spaces, getting data into R using field lengths

2009-09-08 Thread Philipp Pagel
On Tue, Sep 08, 2009 at 02:53:11PM +0300, Lauri Nikkinen wrote: > I have a text file similar to this (separated by spaces): > > x <- "DF12 This is an example 1 This > DF12 This is an 1232 This is > DF14 This is 12334 This is an > DF15 This 23 This is an example > " > > and I know the field length

Re: [R] Data separated by spaces, getting data into R using field lengths

2009-09-08 Thread Duncan Murdoch
On 9/8/2009 8:21 AM, Lauri Nikkinen wrote: This data is from database and the maximum length of a field is defined. I mean that every column has a maximum length and I want to use this maximum length as a separator. So if one "cell" in that column is shorter than the maximum, "cell" should be pad

Re: [R] Data separated by spaces, getting data into R using field lengths

2009-09-08 Thread jim holtman
This bears no relationship to what you were first asking. It look like you want to split the leading 4 characters into two groups of two and then split the remaining data into three parts based on numerics in the middle. Is this correct? On Tue, Sep 8, 2009 at 8:15 AM, Lauri Nikkinen wrote: > Su

Re: [R] Data separated by spaces, getting data into R using field lengths

2009-09-08 Thread Lauri Nikkinen
This data is from database and the maximum length of a field is defined. I mean that every column has a maximum length and I want to use this maximum length as a separator. So if one "cell" in that column is shorter than the maximum, "cell" should be padded with white spaces or something like that.

Re: [R] Data separated by spaces, getting data into R using field lengths

2009-09-08 Thread Ben Bolker
I don't think you described your problem precisely. You implied that you wanted the field lengths to be (2,2,18,5,18) -- which is what you got with read.fwf -- but it looks like what you meant is something more like: field 1: first two characters field 2: characters 3-4 field 3: all alphabeti

Re: [R] Data separated by spaces, getting data into R using field lengths

2009-09-08 Thread Lauri Nikkinen
Sure, here you go structure(list(V1 = structure(c(1L, 1L, 1L, 1L), .Label = "DF", class = "factor"), V2 = c(12L, 12L, 14L, 15L), V3 = structure(c(4L, 3L, 2L, 1L), .Label = c("This", "This is", "This is an", "This is an example" ), class = "factor"), V4 = c(1L, 1232L, 12334L, 23L), V5 =

Re: [R] Data separated by spaces, getting data into R using field lengths

2009-09-08 Thread Duncan Murdoch
On 9/8/2009 8:07 AM, Lauri Nikkinen wrote: Thanks, I tried it but I got varlength <- c(2, 2, 18, 5, 18) read.fwf("c:temppi.txt", widths=varlength) V1 V2 V3V4 V5 1 DF 12 This is an exampl e 1 T his 2 DF 12 This is an 1232 T his is 3 DF 14 This is 12334 Thi s is

Re: [R] Data separated by spaces, getting data into R using field lengths

2009-09-08 Thread jim holtman
Can you post how you would like it. On Tue, Sep 8, 2009 at 8:07 AM, Lauri Nikkinen wrote: > Thanks, I tried it but I got > >> varlength <- c(2, 2, 18, 5, 18) >> read.fwf("c:temppi.txt", widths=varlength) >  V1 V2                 V3    V4   V5 > 1 DF 12  This is an exampl e 1 T  his > 2 DF 12  This

Re: [R] Data separated by spaces, getting data into R using field lengths

2009-09-08 Thread Lauri Nikkinen
Thanks, I tried it but I got > varlength <- c(2, 2, 18, 5, 18) > read.fwf("c:temppi.txt", widths=varlength) V1 V2 V3V4 V5 1 DF 12 This is an exampl e 1 T his 2 DF 12 This is an 1232 T his is 3 DF 14 This is 12334 Thi s is an 4 DF 15 This 23 This is a n exa mple

Re: [R] Data separated by spaces, getting data into R using field lengths

2009-09-08 Thread Duncan Murdoch
On 9/8/2009 7:53 AM, Lauri Nikkinen wrote: I have a text file similar to this (separated by spaces): x <- "DF12 This is an example 1 This DF12 This is an 1232 This is DF14 This is 12334 This is an DF15 This 23 This is an example " and I know the field lengths of each variable (there is 5 variab

Re: [R] Data separated by spaces, getting data into R using field lengths

2009-09-08 Thread Barry Rowlingson
On Tue, Sep 8, 2009 at 12:53 PM, Lauri Nikkinen wrote: > I have a text file similar to this (separated by spaces): > > x <- "DF12 This is an example 1 This > DF12 This is an 1232 This is > DF14 This is 12334 This is an > DF15 This 23 This is an example > " > > and I know the field lengths of each v

[R] Data separated by spaces, getting data into R using field lengths

2009-09-08 Thread Lauri Nikkinen
I have a text file similar to this (separated by spaces): x <- "DF12 This is an example 1 This DF12 This is an 1232 This is DF14 This is 12334 This is an DF15 This 23 This is an example " and I know the field lengths of each variable (there is 5 variables in this data set), which are: varlength