[R] writing an own function - is.factor

jim holtman jholtman at gmail.com
Mon Jan 12 03:30:05 CET 2009


try this:

> d <- data.frame(c(rep("m",5), rep("f",5)), c(1:10))
>  names(d) <- c("x", "y")
>  d
   x  y
1  m  1
2  m  2
3  m  3
4  m  4
5  m  5
6  f  6
7  f  7
8  f  8
9  f  9
10 f 10
> lapply(d, function(x) if (is.numeric(x)) c(mean=mean(x), sd=sd(x)))
$x
NULL

$y
    mean       sd
5.500000 3.027650

>


On Sun, Jan 11, 2009 at 9:04 PM, Jörg Groß <joerg at licht-malerei.de> wrote:
> Hi,
>
> I try to write an own function in R.
> I want a summary table with descriptive statistics.
>
>
> For example, I have this data.frame:
>
>
>  d <- data.frame(c(rep("m",5), rep("f",5)), c(1:10))
>  names(d) <- c("x", "y")
>  d
>         x  y
> 1       m  1
> 2       m  2
> 3       m  3
> 4       m  4
> 5       m  5
> 6       f  6
> 7       f  7
> 8       f  8
> 9       f  9
> 10      f 10
>
>
> now I want to get the mean and sd, as long as the column is not of type
> factor.
> So the function should skip the first column.
>
>
> But how can I check this, if I don't know the column name?
>
> Because
>
> is.factor(d[1])
> produces "FALSE".
>
> Only
> is.factor(d$x)
> gives the correct result.
>
>
> But how can I check the column if I don't know the column name?
>
>
> I tried s.th. like this;
>
> is.factor(d$names(d[1]))
>
>
> but that kind of structure is not possible.
>
>
>
> Can someone help me with that problem?
>
> ______________________________________________
> 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?




More information about the R-help mailing list