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 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" any help is greatly appreciated, gabor
Héctor Villalobos
2008-Nov-27 15:43 UTC
[R] as.numeric in data.frame, but only where it is possible
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 typesPerhaps 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@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@ipn.mx> CICIMAR - IPN La Paz, Baja California Sur, MÉXICO [[alternative HTML version deleted]]
hadley wickham
2008-Nov-27 16:53 UTC
[R] as.numeric in data.frame, but only where it is possible
On Thu, Nov 27, 2008 at 12:53 AM, Kinoko <andzsinszan at gmail.com> 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 > > k<-matrix(c("aa", "bb", 1,2, 4.3, 0), nrow=2) > g<-as.data.frame(k, stringsAsFactos=FALSE)Try this: g <-as.data.frame(k, stringsAsFactors = FALSE) str(g) g[] <- lapply(g, type.convert) str(g) Hadley -- http://had.co.nz/