Is there a way to map data from Binary format to Numerical numbers?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Is there a way to map data from Binary format to Numerical numbers?

memi
This post has NOT been accepted by the mailing list yet.
Hi,
Is there a way to map data from Binary format to Numerical numbers?

example:
I have text files, where each record consists of several items (9 items)
1, means item appear
0, means item absent

1,1,1,0,0,1,1,1,1
1,1,1,0,0,1,0,1,1
1,0,1,1,0,1,1,1,1
1,0,0,0,0,0,0,1,0
1,0,1,0,0,1,1,1,1
1,0,1,1,0,1,0,1,1
1,0,1,0,1,0,0,0,1


I want transform my data to numerical numbers in ascending order, such that when items is absent, i didn't print it, but keep increase the counter. for example, the above binary format will be: ,
1,2,3,6,7,8,9
1,2,3,6,8,9
1,3,4,6,7,8,9
1,8,
1,3,6,7,8,9
1,3,4,5,7,8
1,3,5,9


Reply | Threaded
Open this post in threaded view
|

Re: Is there a way to map data from Binary format to Numerical numbers?

memi
This post has NOT been accepted by the mailing list yet.

I found solution:

A <- readLines(con <- file("1.txt"))
mat<- as.matrix(read.table(text=A,header=FALSE,fill=TRUE))
b<-apply(!!mat ,1,which)
capture.output(writeLines(unlist(lapply(b, paste, collapse=","))), file="num.txt")


But the problem is, my file has 100k lines, and i got error:
Error in !mat : invalid argument type
Reply | Threaded
Open this post in threaded view
|

Re: Is there a way to map data from Binary format to Numerical numbers?

arun kirshna
In reply to this post by memi
Try

indx <- which(!!mat, arr.ind=TRUE)
v1 <-unname(sapply(split(indx[,2], indx[,1]),toString))

cat(paste(v1, collapse="\n"), sep="\n")
1, 2, 3, 6, 7, 8, 9
1, 2, 3, 6, 8, 9
1, 3, 4, 6, 7, 8, 9
1, 8
1, 3, 6, 7, 8, 9
1, 3, 4, 6, 8, 9
1, 3, 5, 9


A.K.

   



Hi,
Is there a way to map data from Binary format to Numerical numbers?

example:
I have text files, where each record consists of several items (9 items)
1, means item appear
0, means item absent

1,1,1,0,0,1,1,1,1
1,1,1,0,0,1,0,1,1
1,0,1,1,0,1,1,1,1
1,0,0,0,0,0,0,1,0
1,0,1,0,0,1,1,1,1
1,0,1,1,0,1,0,1,1
1,0,1,0,1,0,0,0,1


I want transform my data to numerical numbers in ascending order, such that when items is absent, i didn't print it, but keep increase the counter. for example, the above binary format will be: ,
1,2,3,6,7,8,9
1,2,3,6,8,9
1,3,4,6,7,8,9
1,8,
1,3,6,7,8,9
1,3,4,5,7,8
1,3,5,9

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Is there a way to map data from Binary format to Numerical numbers?

memi
This post has NOT been accepted by the mailing list yet.

Thank you so much, its work.

when I tried this code on 100k lines with 9 col, I got the results, but not all of them, the number of lines become 90k instead of 100k
A <- readLines(con <- file("Example21.txt"))
mat1<- as.matrix(read.table(text=A,header=FALSE,fill=TRUE))
b<-apply(!!mat1,1,which)
c<-matrix(b,nrow = 100000,ncol = 9)
capture.output(writeLines(unlist(lapply(c, paste, collapse=" "))), file="NEWnum2.txt")

but with your code, i got it correctly
thank you so much


A <- readLines(con <- file("Example21.txt"))
mat<- as.matrix(read.table(text=A,header=FALSE,fill=TRUE))
indx <- which(!!mat, arr.ind=TRUE)
v1 <-unname(sapply(split(indx[,2], indx[,1]),toString))
capture.output(cat(paste(v1, collapse="\n"), sep="\n") , file="tonum.txt")
Reply | Threaded
Open this post in threaded view
|

Re: Is there a way to map data from Binary format to Numerical numbers?

memi
This post has NOT been accepted by the mailing list yet.
In reply to this post by arun kirshna
Hi

I tried the same code in different data set and i got error
http://store1.up-00.com/2015-04/1430344462541.png

my data consist of 451 line, and the size of line is 280

this code sometimes work, and some times give me the above error

A <- readLines(con <- file("Arrhythmia2.txt"))
mat<- as.matrix(read.table(text=A,header=FALSE,fill=TRUE))
indx2 <- which(!!mat, arr.ind=TRUE)
v1 <-unname(sapply(split(indx2[,2], indx2[,1]),toString))
capture.output(cat(paste(v1, collapse="\n"), sep="\n") , file="Arrhythmia3.txt")
Reply | Threaded
Open this post in threaded view
|

Re: Is there a way to map data from Binary format to Numerical numbers?

memi
This post has NOT been accepted by the mailing list yet.
I knew the reason

in the text file (data set) the separator was sep = ",", I replaced it with " "