[R] as.numeric in data.frame, but only where it is possible
Kinoko
andzsinszan at gmail.com
Thu Nov 27 07:53:09 CET 2008
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
More information about the R-help
mailing list