Java Mailing List Archive

http://www.r-help.com/

Home » Home (12/2007) » R Help for Statistical Computing »

[R] class in apply

Omar Lakkis

2005-07-20

Replies:

Numeric data that is part of a mixed type data frame is converted into
character. How can I tell apply to maintain the original class of a
column and not convert it into character. I would like to do this of
the vector and not inside the apply function individually over each
element. Consider the following two scenarios, in the second column
'a' maintained its class while it lost its numeric type in the first
case.

> df = data.frame(a=c(1,2), b=c('A','B'))
> df
a b
1 1 A
2 2 B
> a=apply(df, 1, function(r) print(class(r['a'])))
[1] "character"
[1] "character"
> a=apply(df, 1, function(r) print(class(r['b'])))
[1] "character"
[1] "character"


> df = data.frame(a=c(1,2))
> df
a
1 1
2 2
> a=apply(df, 1, function(r) print(class(r['a'])))
[1] "numeric"
[1] "numeric"

______________________________________________
R-help@(protected)
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
©2008 r-help.com - Jax Systems, LLC, U.S.A.