[R] Easy 'apply' question

Joshua Wiley jwiley.psych at gmail.com
Fri Mar 11 04:40:33 CET 2011


Dear Aaron,

The problem is not with your function, but using apply().  Look at the
"Details" section of ?apply  You will see that if the data is not an
array or matrix, apply will coerce it to one (or try).  Now go over to
the "Details" section of ?matrix and you will see that matrices can
only contain a single class of data and that this follows a hierarchy.
 In short, your data frame is coerced to a data frame and the classes
are all coerced to the highest---character.  You can use lapply()
instead to get your desired results.  Here is an example:

## Construct (named) test dataframe
tf <- data.frame(x = 1:3, y = 4:6, z = c("A","A","A"))

## Show why what you tried did not work
(test <- apply(tf, 2, class))

## using lapply()
(test <- lapply(tf, function(x) {
  if(is.numeric(x)) mean(x) else unique(x)[1]}))


Hope this helps,

Josh

On Thu, Mar 10, 2011 at 5:11 PM, Aaron Polhamus <aaronpolhamus at gmail.com> wrote:
> Dear list,
>
> I couldn't find a solution for this problem online, as simple as it seems.
> Here's the problem:
>
>
> #Construct test dataframe
> tf <- data.frame(1:3,4:6,c("A","A","A"))
>
> #Try the apply function I'm trying to use
> test <- apply(tf,2,function(x) if(is.numeric(x)) mean(x) else unique(x)[1])
>
> #Look at the output--all columns treated as character columns...
> test
>
> #Look at the format of the original data--the first two columns are
> integers.
> str(tf)
>
>
> In general terms, I want to differentiate what function I apply over a
> row/column based on what type of data that row/column contains. Here I want
> a simple mean if the column is numeric, and the first unique value if the
> column is a character column. As you can see, 'apply' treats all columns as
> characters the way I've written his function.
>
> Any thoughts? Many thanks in advance,
> Aaron
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list