[R] read.table input array
jim holtman
jholtman at gmail.com
Fri Jul 16 18:27:15 CEST 2010
Here is a way of creating a separate list of variable length vectors
that you can use in your processing:
> # read into a dataframe
> x <- read.table(textConnection("A B C T Lengths
+ 1 4.0 0.0015258789 18 c(1,2,3)
+ 1 4.0 0.0015258789 18 c(1,2,6,7,8,3)
+ 1 4.0 0.0015258789 18 c(1,2,3,1,2,3,4,5,6,7,9)
+ 1 4.0 0.0015258789 18 c(1,2,3)
+ 1 1.0 0.0017166138 24 c(1,1,4)"), header=TRUE)
> # create a 'list' with the variable length vectors
> # assuming the the "Lengths" are legal R expressions using 'c'
> x$varList <- lapply(x$Lengths, function(a) eval(parse(text=a)))
>
> x
A B C T Lengths varList
1 1 4 0.001525879 18 c(1,2,3) 1, 2, 3
2 1 4 0.001525879 18 c(1,2,6,7,8,3) 1, 2, 6, 7, 8, 3
3 1 4 0.001525879 18 c(1,2,3,1,2,3,4,5,6,7,9) 1, 2, 3, 1, 2, 3, 4, 5, 6, 7, 9
4 1 4 0.001525879 18 c(1,2,3) 1, 2, 3
5 1 1 0.001716614 24 c(1,1,4) 1, 1, 4
> str(x)
'data.frame': 5 obs. of 6 variables:
$ A : int 1 1 1 1 1
$ B : num 4 4 4 4 1
$ C : num 0.00153 0.00153 0.00153 0.00153 0.00172
$ T : int 18 18 18 18 24
$ Lengths: Factor w/ 4 levels "c(1,1,4)","c(1,2,3)",..: 2 4 3 2 1
$ varList:List of 5
..$ : num 1 2 3
..$ : num 1 2 6 7 8 3
..$ : num 1 2 3 1 2 3 4 5 6 7 ...
..$ : num 1 2 3
..$ : num 1 1 4
>
On Fri, Jul 16, 2010 at 10:51 AM, Balpo <balpo at gmx.net> wrote:
> Hello to all!
> I am new with R and I need your help.
> I'm trying to read a file which contests are similar to this:
> A B C T Lengths
> 1 4.0 0.0015258789 18 c(1,2,3)
> 1 1.0 0.0017166138 24 c(1,1,4)
>
> So all the columns are numeric values, except Lengths, which is supposed to
> be an variable length array of integers.
> How can I make R read them as arrays of integers? Or otherwise, convert the
> character array to an array of integers.
> When I read the file, I do it like this
> t1 = read.table(file=paste("./borrar.dat",sep=""), header=T,
> colClasses=c("numeric", "numeric", "numeric", "numeric", "array"))
> But the 5th column is treated as an array of characters, and when trying to
> convert it to another class of data, I either
> get two strings "c(1,2,3)" and "c(1,1,4)" or using a toRaw converter, I get
> the corresponding ASCII ¿? values.
> Should the input be modified in order to be able to read it as an array of
> integers?
>
> Thank you for your help.
> Balpo
>
> ______________________________________________
> 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?
More information about the R-help
mailing list