From: Héctor Villalobos <hvillalo_at_ipn.mx>

Date: Thu, 27 Nov 2008 08:43:05 -0700

Date: Thu, 27 Nov 2008 08:43:05 -0700

Hello,

On 26 Nov 2008 at 22:53, Kinoko wrote:

*> Hi,
**>
*

> I would like to convert my "character" sequences in my matrix/

*> data.frame into "numeric" where it is possible.
**> I would also like to retain my alphabetic character strings in their
**> original forms. "5.1" > 5.1 "hm" > "hm"
**>
**> k<-matrix(c("aa", "bb", 1,2, 4.3, 0), nrow=2)
**> mode(k) <- "numeric"
**> # ln1 coerces numeric chars into "character" and
**> # ln2 replaces alphabet chars with NA (with warnings)
**> # => OK as matrix can't have mixed types
*

Perhaps there is a simpler solution, but this should work:

k <- matrix(c("aa", "bb", 1 ,2, 4.3, 0), nrow=2)

write.table(k,"'xx.txt", col.names=TRUE, row.names=FALSE, sep="'\t", quote=FALSE) k <- read.table("xx.txt", header=TRUE, sep="\t") summary(k)

*>
**> k<-matrix(c("aa", "bb", 1,2, 4.3, 0), nrow=2)
*

> g<-as.data.frame(k, stringsAsFactos=FALSE)

*> g[,2]
**> # returns:
**> # [1] 1 2
**> # Levels: 1 2
**> # hm... let them be levels then...
**>
**> sum(g[,2])
**> # going down in flames
**>
**> g[,2] <- as.numeric(g[,2])
**> sum(g[,2])
**> # is the winner of the day,
**> # but I have a hunch that there must be something more trivial/general
**> solution.
**>
**> - I'm looking for something like, as.data.frame(...
**> as.numeric.if.possible=TRUE) ,
**> if possible.
**> - Mainly, I don't know in advance if a column has alphabetical or
**> numeric characters. - I have relatively large matrices (or a
**> relatively slow PC) (max 100,000 x 100 "cells"), if that matters
**>
**> *** *** *** *** ***
**> Another related problem of mine is to create data.frames with mixed
**> types (it should be possible, shouldn't it).
**>
**> d<-data.frame("b"=seq(1,3))
**> d<-cbind(a,b)
**> typeof(d)
**> # "d" was coerced into "character"
*

## In this case you can do:

a <- c("aa", "bb", "cc")

d <- data.frame(a=a, b=seq(1, 3))

summary(d)

## or

d <- data.frame(b=seq(1, 3))

d$a <- c("aa", "bb", "cc")

summary(d)

*>
**>
*

> any help is greatly appreciated,

*>
**> gabor
**>
**> ______________________________________________
**> 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.
**>
*

-- Héctor Villalobos <hvillalo_at_ipn.mx> CICIMAR - IPN La Paz, Baja California Sur, MÉXICO [[alternative HTML version deleted]]Received on Thu 27 Nov 2008 - 16:36:47 GMT______________________________________________ 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.

Archive maintained by Robert King, hosted by
the discipline of
statistics at the
University of Newcastle,
Australia.

Archive generated by hypermail 2.2.0, at Thu 27 Nov 2008 - 17:30:28 GMT.

*
Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help.
Please read the posting
guide before posting to the list.
*