[R] converting values of a dataframe to numeric (where possible)

Ralikwen Balazs.Klein at gmail.com
Wed Sep 3 13:13:38 CEST 2008


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



Ralikwen 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:
>> 
>> Try this as a solution:
>> 
>>> df <- data.frame(a=letters[15:17], b=c("21","NA","23"), c=10:12,
>>> d=15:17)
>>> # convert to numeric
>>> x <- as.matrix(df)
>>> mode(x) <- "numeric"
>> Warning message:
>> In eval(expr, envir, enclos) : NAs introduced by coercion
>>> cbind(df, RTot=rowSums(x, na.rm=TRUE))
>>   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:
>>>
>>> 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
>>>
>>>
>>>
>>> --
>>> View this message in context:
>>> http://www.nabble.com/converting-values-of-a-dataframe-to-numeric-%28where-possible%29-tp19279139p19279139.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> 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.
>>>
>> 
>> 
>> 
>> -- 
>> Jim Holtman
>> Cincinnati, OH
>> +1 513 646 9390
>> 
>> What is the problem that you are trying to solve?
>> 
>> ______________________________________________
>> 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.
>> 
>> 
> 
> 

-- 
View this message in context: http://www.nabble.com/converting-values-of-a-dataframe-to-numeric-%28where-possible%29-tp19279139p19287188.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list