Data management problem: convert text string to matrix of 0's and 1's

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

Data management problem: convert text string to matrix of 0's and 1's

Dale Steele-2
I have a data management problem which exceeds my meager R programming
skills and would greatly appreciate suggestions on how to proceed?  The
data consists of a series of observation periods. Specific behaviors are
recorded for each time period in the order each is observed.  Their are
8 possible behaviors, coded as "i" "c" "s" "r" "v" "e" "p" "f".

The data looks like:
-->
icsrvepf
fpevrsci
ics
p

f
ic
<--

I would like to convert the about to a matrix of the form:

  i c s r v e p f
  1 1 1 1 1 1 1 1
  1 1 1 1 1 1 1 1
  1 1 1 0 0 0 0 0
  0 0 0 0 0 0 1 0
  0 0 0 0 0 0 0 0
  0 0 0 0 0 0 0 1
  1 1 0 0 0 0 0 0

Thanks.

Dale

Dale Steele, MD
Pediatric Emergency Medicine
Brown Medical School

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Data management problem: convert text string to matrix of 0's and 1's

Thomas Lumley
On Thu, 26 Jan 2006, Dale Steele wrote:

> The data looks like:
> -->
> icsrvepf
> fpevrsci
> ics
> p
>
> f
> ic
> <--
>
> I would like to convert the about to a matrix of the form:
>
>  i c s r v e p f
>  1 1 1 1 1 1 1 1
>  1 1 1 1 1 1 1 1
>  1 1 1 0 0 0 0 0
>  0 0 0 0 0 0 1 0
>  0 0 0 0 0 0 0 0
>  0 0 0 0 0 0 0 1
>  1 1 0 0 0 0 0 0
>

One possibility is to use grep()
> a
[1] "icsrvepf" "fpevrsci" "p"        ""         "f"        "ic"
> grep("i",a)
[1] 1 2 6

so
> results<-matrix(0,nrow=length(a),ncol=length(behaviours))
> colnames(results)<-behaviours
> for(b in behaviours) results[grep(b,a),b]<-1
> results
      i c s r v e p f
[1,] 1 1 1 1 1 1 1 1
[2,] 1 1 1 1 1 1 1 1
[3,] 0 0 0 0 0 0 1 0
[4,] 0 0 0 0 0 0 0 0
[5,] 0 0 0 0 0 0 0 1
[6,] 1 1 0 0 0 0 0 0
>

  -thomas

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Data management problem: convert text string to matrix of 0's and 1's

jholtman
Here is another way:

> x <- scan('/tempxx.txt', what='', sep='\n', blank.lines.skip=F)
Read 7 items
> x
[1] "icsrvepf" "fpevrsci" "ics"      "p"        ""         "f"
"ic"
> x <- strsplit(x, '') # break into single characters
> template <- c(i=0, c=0, s=0, r=0, v=0, e=0, p=0, f=0)
> mat <- lapply(x, function(.l){
+     .result <- template # initialize the result
+     .result[.l] <- 1
+     .result
+ })
> do.call('rbind', mat)
     i c s r v e p f
[1,] 1 1 1 1 1 1 1 1
[2,] 1 1 1 1 1 1 1 1
[3,] 1 1 1 0 0 0 0 0
[4,] 0 0 0 0 0 0 1 0
[5,] 0 0 0 0 0 0 0 0
[6,] 0 0 0 0 0 0 0 1
[7,] 1 1 0 0 0 0 0 0
>



On 1/26/06, Thomas Lumley <[hidden email]> wrote:

>
> On Thu, 26 Jan 2006, Dale Steele wrote:
> > The data looks like:
> > -->
> > icsrvepf
> > fpevrsci
> > ics
> > p
> >
> > f
> > ic
> > <--
> >
> > I would like to convert the about to a matrix of the form:
> >
> >  i c s r v e p f
> >  1 1 1 1 1 1 1 1
> >  1 1 1 1 1 1 1 1
> >  1 1 1 0 0 0 0 0
> >  0 0 0 0 0 0 1 0
> >  0 0 0 0 0 0 0 0
> >  0 0 0 0 0 0 0 1
> >  1 1 0 0 0 0 0 0
> >
>
> One possibility is to use grep()
> > a
> [1] "icsrvepf" "fpevrsci" "p"        ""         "f"        "ic"
> > grep("i",a)
> [1] 1 2 6
>
> so
> > results<-matrix(0,nrow=length(a),ncol=length(behaviours))
> > colnames(results)<-behaviours
> > for(b in behaviours) results[grep(b,a),b]<-1
> > results
>      i c s r v e p f
> [1,] 1 1 1 1 1 1 1 1
> [2,] 1 1 1 1 1 1 1 1
> [3,] 0 0 0 0 0 0 1 0
> [4,] 0 0 0 0 0 0 0 0
> [5,] 0 0 0 0 0 0 0 1
> [6,] 1 1 0 0 0 0 0 0
> >
>
>        -thomas
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>



--
Jim Holtman
Cincinnati, OH
+1 513 247 0281

What the problem you are trying to solve?

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Data management problem: convert text string to matrix of 0's and 1's

Martin Lam
In reply to this post by Dale Steele-2
Hi Dale,

Unfortunately, you didn't say in what for format your
data is saved into, so I presume it's saved as a list
of strings. Perhaps there is a faster/better way, but
this should suffice if your datasize isn't enormous.

data = list()
data[1] = "icsrvepf"
data[2] = "fpevrsci"
data[3] = "ics"
data[4] = "p"
data[5] = ""
data[6] = "f"
data[7] = "ic"

names = as.character(c("i", "c", "s", "r", "v", "e",
"p", "f"))

mymatrix = matrix(0, nrow = 7, ncol = 8)
colnames(mymatrix) = names

for (i in 1:length(data)) {
  # split the string into separate characters
  chars = strsplit(data[[i]], split="")[[1]]

  mymatrix[i,which(names %in%chars)] = 1
}
mymatrix

HTH,

Martin Lam

--- Dale Steele <[hidden email]> wrote:

> I have a data management problem which exceeds my
> meager R programming
> skills and would greatly appreciate suggestions on
> how to proceed?  The
> data consists of a series of observation periods.
> Specific behaviors are
> recorded for each time period in the order each is
> observed.  Their are
> 8 possible behaviors, coded as "i" "c" "s" "r" "v"
> "e" "p" "f".
>
> The data looks like:
> -->
> icsrvepf
> fpevrsci
> ics
> p
>
> f
> ic
> <--
>
> I would like to convert the about to a matrix of the
> form:
>
>   i c s r v e p f
>   1 1 1 1 1 1 1 1
>   1 1 1 1 1 1 1 1
>   1 1 1 0 0 0 0 0
>   0 0 0 0 0 0 1 0
>   0 0 0 0 0 0 0 0
>   0 0 0 0 0 0 0 1
>   1 1 0 0 0 0 0 0
>
> Thanks.
>
> Dale
>
> Dale Steele, MD
> Pediatric Emergency Medicine
> Brown Medical School
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html