The second beginner question. I want to create a new dataframe, where each element of the original dataframe is translated to 1 if it was "+", to 0 if it was "-" to -1 otherwise. I could do with: Lines <- "a b c d + - + + + + + - + 1 - '+ ' - + + + + N - +" DF <- read.table(textConnection(Lines), header = TRUE) cnames <- colnames(DF) nrow <-length(rownames(DF)) nc <- length(cnames) NDF <- data.frame(matrix(c(rep(0,nc*nrow)),ncol=nc)) for (i in 1:length(cnames)) { NDF[,i] <- sapply(DF[,i],function(x) if (x=="+") {1} else {if (x=="-") {0} else {-1}} ) } colnames(NDF) <- cnames But this is shure one loop too much. Please give me the R way solution.
On 4/16/2009 2:58 PM, Juergen Rose wrote:> The second beginner question. I want to create a new dataframe, where > each element of the original dataframe is translated to 1 if it was "+", > to 0 if it was "-" to -1 otherwise. I could do with: > > Lines <- "a b c d > + - + + > + + + - > + 1 - '+ ' > - + + + > + N - +" > > > DF <- read.table(textConnection(Lines), header = TRUE) > cnames <- colnames(DF) > nrow <-length(rownames(DF)) > > nc <- length(cnames) > NDF <- data.frame(matrix(c(rep(0,nc*nrow)),ncol=nc)) > > for (i in 1:length(cnames)) { > NDF[,i] <- sapply(DF[,i],function(x) if (x=="+") {1} else {if (x=="-") > {0} else {-1}} ) > } > colnames(NDF) <- cnames > > But this is shure one loop too much. Please give me the R way solution.library(car) DF[] <- lapply(DF, function(x){recode(x, "'+'=1; '-'=0; else=-1")})> ______________________________________________ > 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.-- Chuck Cleland, Ph.D. NDRI, Inc. (www.ndri.org) 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
Try this: 2 * (DF == "+") - (DF != "-") On Thu, Apr 16, 2009 at 2:58 PM, Juergen Rose <rose at rz.uni-potsdam.de> wrote:> The second beginner question. I want to create a new dataframe, where > each element of the original dataframe is translated to 1 if it was "+", > to 0 if it was "-" to -1 otherwise. I could do with: > > Lines <- "a ? ?b ? ?c ? ?d > ? ?+ ? ?- ? ?+ ? + > ? ?+ ? ?+ ? ?+ ? - > ? ?+ ? ?1 ? ?- ? '+ ' > ? ?- ? ?+ ? ?+ ? + > ? ?+ ? ?N ? ?- ? +" > > > DF <- read.table(textConnection(Lines), header = TRUE) > cnames <- colnames(DF) > nrow <-length(rownames(DF)) > > nc <- length(cnames) > NDF <- data.frame(matrix(c(rep(0,nc*nrow)),ncol=nc)) > > for (i in 1:length(cnames)) { > ?NDF[,i] <- sapply(DF[,i],function(x) if (x=="+") {1} else {if (x=="-") > {0} else {-1}} ) > } > colnames(NDF) <- cnames > > But this is shure one loop too much. Please give me the R way solution. > > ______________________________________________ > 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. >
Maybe Matching Threads
- I need help making a data.fame comprised of selected columns of an original data frame.
- Troubles with DBI's dbWriteTable in RMySQL
- install a package made using bioconductor package pdInfoBuilder
- Problem installing ndf library
- Error:non-numeric argument in my function