Try: as.numeric(as.character( typec))

milton
On Mon, Mar 8, 2010 at 12:55 PM, Xumin Zeng <xumin.z...@abbott.com> wrote:

> Here is the example.
>
> > age=18:29
> > height=c(76.1,77,78.1,78.2,78.8,79.7,79.9,81.1,81.2,81.8,82.8,83.5)
> > type=c("A", "B", "C", "D","A", "B", "C", "D","A", "B", "C", "D")
> > typec=c("0","4","2","9","0","7","2","3","0","1","2","3")
> > typen=c(0,1,2,3,0,1,2,3,0,1,2,3)
> > data1=data.frame(age=age,height=height, type=type, typec=typec,
> typen=typen)
>
> > data1[,3]=as.numeric(data1[,3])
> > data1[,4]=as.numeric(data1[,4])
> > data1[,5]=as.numeric(data1[,5])
>
> > print(data1)
>
> and I got the output as:
>
>   age height type typec typen
> 1   18   76.1    1     1     0
> 2   19   77.0    2     5     1
> 3   20   78.1    3     3     2
> 4   21   78.2    4     7     3
> 5   22   78.8    1     1     0
> 6   23   79.7    2     6     1
> 7   24   79.9    3     3     2
> 8   25   81.1    4     4     3
> 9   26   81.2    1     1     0
> 10  27   81.8    2     2     1
> 11  28   82.8    3     3     2
> 12  29   83.5    4     4     3
>
> The "typec" is not what I expected. How can I get the direct conversion
> from character to numeric and get the following output?
>
>   age height type typec typen
> 1   18   76.1    1     0     0
> 2   19   77.0    2     4     1
> 3   20   78.1    3     2     2
> 4   21   78.2    4     9     3
> 5   22   78.8    1     0     0
> 6   23   79.7    2     7     1
> 7   24   79.9    3     2     2
> 8   25   81.1    4     3     3
> 9   26   81.2    1     0     0
> 10  27   81.8    2     1     1
> 11  28   82.8    3     2     2
> 12  29   83.5    4     3     3
>
> Thanks.
>
> Xumin
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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<http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

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

Reply via email to