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?
try this:> d <- data.frame(c(rep("m",5), rep("f",5)), c(1:10)) > names(d) <- c("x", "y") > dx 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?
On Sun, Jan 11, 2009 at 9:04 PM, Jörg Groß <joerg@licht-malerei.de> wrote:> ...now I want to get the mean and sd, as long as the column is not of type > factor. > ...But how can I check this, if I don't know the column name? >...> is.factor(d[1]) > produces "FALSE". >Try is.factor(d[[1]]). Remember that in R, x[...] selects a *subsequence*, not an *element*. Only x[[...]] selects an element. What makes this confusing to learn is that for vectors, a subsequence of length 1 is treated the same as an element. But for lists, they are not the same thing. You can iterate over the columns in various ways, e.g. for (i in 1:length(d)) ... d[[i]] ... for (i in names(d)) ... d[[i]] ...> > Only > is.factor(d$x) > gives the correct result. >Remember that d$x == d[["x"]] Hope this helps, -s [[alternative HTML version deleted]]