Inside the apply function, near the top, we see:
d <- dim(X)
if((dlen <- length(d)) == 0)
stop("dim(X) must have a positive length")
if(length(class(X))) {
if(dlen == 2)
X <- as.matrix(X)
else X <- as.array(X)
So if the length of the dim(X) is 2 then as.matrix is applied.
Now,
as.matrix(fjj)
A B C D
1 "1" "NA" "FALSE" "NA"
2 "2" "NA" "FALSE" "NA"
3 "3" "NA" "FALSE" "NA"
4 "4" "NA" "FALSE" "NA"
5 "5" "NA" "FALSE" "NA"
Turns all to character, which are not null:
is.na(as.matrix(fjj))
A B C D
1 F F F F
2 F F F F
3 F F F F
4 F F F F
5 F F F F
Note, that as defined (with auto convert to factors turned on)
is.factor(fjj[,2])
[1] T
That is, a column of all nulls turns into a factor when using data frames.
Now if we define fjj as:
fjj <- data.frame(A = 1:5, B = as.numeric(rep(NA,5)), C = logical(5), D =
as.numeric(rep(NA, 5)))
then,
as.matrix(fjj[,1:2])
A B
1 1 NA
2 2 NA
3 3 NA
4 4 NA
5 5 NA
stays numeric, and is only when we include the logical column that again
we get all characters:
> as.matrix(fjj[,1:3])
A B C
1 "1" "NA" "FALSE"
2 "2" "NA" "FALSE"
3 "3" "NA" "FALSE"
4 "4" "NA" "FALSE"
5 "5" "NA" "FALSE"
-John
On Wed, 17 Feb 1999 Gregciresi@aol.com wrote:
>
> I was trying to identify columns in a data frame that contained all missing
> values, and thought
> to apply a function over its columns.
>
> fjj <- data.frame(A = 1:5, B = rep(NA,5), C = logical(5), D = rep(NA, 5))
> > fjj
> A B C D
> 1 1 NA FALSE NA
> 2 2 NA FALSE NA
> 3 3 NA FALSE NA
> 4 4 NA FALSE NA
> 5 5 NA FALSE NA
> > apply(fjj, 2, function(x) all(is.na(x)))
> A B C D
> F F F F
> > sapply(fjj, function(x) all(is.na(x)))
> A B C D
> F T F T
>
> Does anyone know why sapply works instead of apply (i.e., the difference
> between them
> as applied to data frames)? Thanks in advance.
>
> Regards,
> Greg Ciresi
>
> gregciresi@aol.com
>
> -----------------------------------------------------------------------
> This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
> send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
> message: unsubscribe s-news
>
--
John Wallace
University of Washington ^ ^ ^
Fisheries Research Institute / \ / \ / \ ^
Box 357980 / \ / \ | / \
Seattle, WA 98195-7980 | | o__~ / \
PHONE (206) 543-1513 @ @ /\/\ |
FAX (206) 685-7471 ~
E-MAIL jw@u.washington.edu o
WWW http://www.fish.washington.edu/people/jrw/Wallace.html
o _///_ //
<`)= _<<
\\\ \\
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news
|