[R] Help R
jim holtman
jholtman at gmail.com
Thu Jun 17 15:56:06 CEST 2010
try this:
> x <- textConnection(" A,B,C,D,G
+ A,C,E,O
+ F,G")
> # assume you read in your data by Lines since not the same number of fields in each line
> input <- readLines(x)
> close(x)
> # remove blanks that might be there
> input <- gsub(' *', '', input)
> # split by comma
> in.s <- strsplit(input, ',')
> # determine the range of items for the array
> id <- sort(unique(unlist(in.s)))
> # create the output matrix
> output <- matrix(0, ncol=length(id), nrow=length(in.s))
> colnames(output) <- id
> # now iterate and set values
> for (i in seq_along(in.s)){
+ output[i, unlist(in.s[[i]])] <- 1
+ }
>
>
>
> output
A B C D E F G O
[1,] 1 1 1 1 0 0 1 0
[2,] 1 0 1 0 1 0 0 1
[3,] 0 0 0 0 0 1 1 0
>
On Thu, Jun 17, 2010 at 6:10 AM, <ricardosousa2000 at clix.pt> wrote:
>
> Hello,
> I'm new in using the R, but from what I read is an excellent tool.
> Would you like if I could help, I am trying create an array from reading
> a text file.
> The idea is to read the file, and transform the data in binary format,
> for example. The calves of this file format.
> A,B,C,D,G
> A,C,E,O
> F,G
> Put this away
> a b c d e f g o
> 1 1 1 1 1 0 0 1 0
> 2 1 0 1 0 1 0 0 1
> 3 0 0 0 0 0 1 0 0
> and display in monitor.
> Thanks for the help
> _________________________________________________________________
>
> Ãrea de Clientes Optimus Clix â Toda a gestão dos seus serviços online!
> http://cliente.clix.pt/.
> ______________________________________________
> 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