Seems like you got your input and output mixed up a bit...
> From: Jack Tanner
>
> I want to iterate over a data frame by columns, and as I'm processing
> each column I want to know its column name.
>
> > a <- as.data.frame(list(1,2,3))
> > colnames(a) <- c("a", "b", "c")
> > colnames(a)
You must have the two lines above reversed, for the output you get below
suggests so.
> [1] "X1" "X2" "X3"
> > lapply(a, function(x) {print(colnames(x))})
> NULL
> NULL
> NULL
> $a
> NULL
>
> $b
> NULL
>
> $c
> NULL
What happened is, essentially:
for (i in seq(along=a)) {
print(colnames(a[[i]]))
}
so now you see what's wrong: colnames() of any columns in `a' is NULL,
because a single column of `a' is no longer a data frame, and thus has no
colnames. So R prints `NULL' three times. The final output is from
lapply(), which is a list with the same names as the input list, but the
components having values of the function being lapply()'ed: NULL.
Andy
> What is lapply doing? Why does it drop the column name of
> every column
> it's iterating over? How can I get the column name as a string?
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>
>