Skip to content

converting values of a dataframe to numeric (where possible)

5 messages · jim holtman, Ralikwen

#
Hi,
I am new to R.
I have a dataframe with many columns some of which contain genuine strings
some numerical values as strings. The dataframe is created by cast so I have
no control over the resulting data type.
I want to attach columns as aggregates of other columns to the dataframe.
Here is the solution that I came up with (after a lot of struggle):

castNum <- function(n) {
    x<-as.numeric(as.character(n))
    if (is.na(x)){      
      return(n)  
    }else{
      return(x)
    }
}
df <- data.frame(a=letters[15:17], b=c("21","NA","23"), c=10:12, d=15:17)
cbind(df,RTot=rowSums(as.data.frame(lapply(df, function(x)
castNum(x)))[2:4],na.rm=TRUE))

This works, but is full of warnings and looks extremely ugly.
Could you direct me how to achieve the same result in a more elegant way?

Thx.
Bal?zs
#
Try this as a solution:
Warning message:
In eval(expr, envir, enclos) : NAs introduced by coercion
a  b  c  d RTot
1 o 21 10 15   46
2 p NA 11 16   27
3 q 23 12 17   52

        
On Tue, Sep 2, 2008 at 5:50 PM, Ralikwen <Balazs.Klein at gmail.com> wrote:

  
    
#
This is what I was looking for.
Using mode instead of as.numeric is a great idea.
Many thanks.
Bal?zs
jholtman wrote:

  
    
#
When I do as.matrix I loose those columns that I specified as row headers
during cast.
Maybe its because of this:
"When coercing a vector, it produces a one-column matrix, and promotes the
names (if any) of the vector to the rownames of the matrix."
but I cant figure out what does this mean or what to do to make it work.

Thanks for the help.
B.

df <- data.frame(
  entityID=c(10,10,10,12,12,12),
  attributeID=c("attr1","attr2","attr3","attr1","attr2","attr3"),
  value=c(10,11,12,"aa",21,22)
)
x<-cast(df,entityID ~ attributeID)
x
as.matrix(x)
jholtman wrote:

  
    
#
Ok, I think I have this ....
it works if I use data.frame(cast(df,entityID ~ attributeID))
Ralikwen wrote: