Dear List, how can I coerce a matrix like this [,1] [,2] [,3] [,4] [,5] [,6] [1,] "0" "1" "1" "0" "0" "0" [2,] "1" "0" "1" "0" "0" "0" [3,] "1" "1" "0" "0" "0" "0" [4,] "0" "0" "0" "0" "1" "0" [5,] "0" "0" "0" "1" "0" "0" [6,] "0" "0" "0" "0" "0" "0" to be filled with numbers? this is the result of replacing some character ("v", "d") with 0 and 1, using the code I found with RSiteSearch() z[] <- lapply(z, factor, levels = c("d", "v"), labels = c(0, 1)); thank you, Simone |-------------------------------------------------| dott. Simone Gabbriellini PhD Student Dipartimento di Scienze Sociali Università di Pisa via Colombo 35 - 56100 Pisa mail: simone.gabbriellini@sp.unipi.it mobile: +39 3475710037 |-------------------------------------------------| Please avoid sending me Word or PowerPoint attachments. See http://www.gnu.org/philosophy/no-word-attachments.html [[alternative HTML version deleted]]
On Tue, 2006-09-12 at 18:42 +0200, Simone Gabbriellini wrote:> Dear List, > > how can I coerce a matrix like this > > [,1] [,2] [,3] [,4] [,5] [,6] > [1,] "0" "1" "1" "0" "0" "0" > [2,] "1" "0" "1" "0" "0" "0" > [3,] "1" "1" "0" "0" "0" "0" > [4,] "0" "0" "0" "0" "1" "0" > [5,] "0" "0" "0" "1" "0" "0" > [6,] "0" "0" "0" "0" "0" "0" > > to be filled with numbers? > > this is the result of replacing some character ("v", "d") with 0 and > 1, using the code I found with RSiteSearch() > > z[] <- lapply(z, factor, levels = c("d", "v"), labels = c(0, 1)); > > thank you, > SimoneI reverse engineered your (presumably) original data frame:> z1 2 3 4 5 6 1 d v v d d d 2 v d v d d d 3 v v d d d d 4 d d d d v d 5 d d d v d d 6 d d d d d d> str(z)`data.frame': 6 obs. of 6 variables: $ 1: Factor w/ 2 levels "d","v": 1 2 2 1 1 1 $ 2: Factor w/ 2 levels "d","v": 2 1 2 1 1 1 $ 3: Factor w/ 2 levels "d","v": 2 2 1 1 1 1 $ 4: Factor w/ 2 levels "d","v": 1 1 1 1 2 1 $ 5: Factor w/ 2 levels "d","v": 1 1 1 2 1 1 $ 6: Factor w/ 2 levels "d","v": 1 1 1 1 1 1 If that is correct, then the following should yield what you want in one step:> z.num <- sapply(z, function(x) as.numeric(x) - 1)> z.num1 2 3 4 5 6 [1,] 0 1 1 0 0 0 [2,] 1 0 1 0 0 0 [3,] 1 1 0 0 0 0 [4,] 0 0 0 0 1 0 [5,] 0 0 0 1 0 0 [6,] 0 0 0 0 0 0> str(z.num)num [1:6, 1:6] 0 1 1 0 0 0 1 0 1 0 ... - attr(*, "dimnames")=List of 2 ..$ : NULL ..$ : chr [1:6] "1" "2" "3" "4" ... Alternatively, if you were starting out with the character matrix:> z.char[,1] [,2] [,3] [,4] [,5] [,6] [1,] "0" "1" "1" "0" "0" "0" [2,] "1" "0" "1" "0" "0" "0" [3,] "1" "1" "0" "0" "0" "0" [4,] "0" "0" "0" "0" "1" "0" [5,] "0" "0" "0" "1" "0" "0" [6,] "0" "0" "0" "0" "0" "0" You could do:> storage.mode(z.char) <- "numeric"> z.char[,1] [,2] [,3] [,4] [,5] [,6] [1,] 0 1 1 0 0 0 [2,] 1 0 1 0 0 0 [3,] 1 1 0 0 0 0 [4,] 0 0 0 0 1 0 [5,] 0 0 0 1 0 0 [6,] 0 0 0 0 0 0> str(z.char)num [1:6, 1:6] 0 1 1 0 0 0 1 0 1 0 ... - attr(*, "dimnames")=List of 2 ..$ : NULL ..$ : NULL Yet another alternative:> matrix(as.numeric(z.char), dim(z.char))[,1] [,2] [,3] [,4] [,5] [,6] [1,] 0 1 1 0 0 0 [2,] 1 0 1 0 0 0 [3,] 1 1 0 0 0 0 [4,] 0 0 0 0 1 0 [5,] 0 0 0 1 0 0 [6,] 0 0 0 0 0 0 HTH, Marc Schwartz
if only 2 letters: (z=="v")*1 else: lapply(z, function(x) as.numeric(as.character(factor(x,levels= c("d","v","w"),labels=c(0,1,2))))) ------------------------------------------------------------------- Jacques VESLOT CNRS UMR 8090 I.B.L (2?me ?tage) 1 rue du Professeur Calmette B.P. 245 59019 Lille Cedex Tel : 33 (0)3.20.87.10.44 Fax : 33 (0)3.20.87.10.31 http://www-good.ibl.fr ------------------------------------------------------------------- Simone Gabbriellini a ?crit :> Dear List, > > how can I coerce a matrix like this > > [,1] [,2] [,3] [,4] [,5] [,6] > [1,] "0" "1" "1" "0" "0" "0" > [2,] "1" "0" "1" "0" "0" "0" > [3,] "1" "1" "0" "0" "0" "0" > [4,] "0" "0" "0" "0" "1" "0" > [5,] "0" "0" "0" "1" "0" "0" > [6,] "0" "0" "0" "0" "0" "0" > > to be filled with numbers? > > this is the result of replacing some character ("v", "d") with 0 and > 1, using the code I found with RSiteSearch() > > z[] <- lapply(z, factor, levels = c("d", "v"), labels = c(0, 1)); > > thank you, > Simone > > |-------------------------------------------------| > > dott. Simone Gabbriellini > PhD Student > Dipartimento di Scienze Sociali > Universit? di Pisa > via Colombo 35 - 56100 Pisa > mail: simone.gabbriellini at sp.unipi.it > mobile: +39 3475710037 > > |-------------------------------------------------| > > Please avoid sending me Word or PowerPoint attachments. > See http://www.gnu.org/philosophy/no-word-attachments.html > > > [[alternative HTML version deleted]] > > > > ------------------------------------------------------------------------ > > ______________________________________________ > R-help at stat.math.ethz.ch 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.