[R] sapply again return value
Mark Lyman
mark.lyman at gmail.com
Fri Feb 16 22:36:58 CET 2007
Antje <niederlein-rstat <at> yahoo.de> writes:
> Hello,
>
> I used an sapply to get some data back (s <- sapply(...) ). The output
> of s would then deliver something like this:
>
> B06_lamp.csv C06_lamp.csv D06_lamp.csv
> [1,] NULL NULL Numeric,512
> [2,] NULL NULL Numeric,512
> [3,] NULL NULL 2
> > mode(s)
> [1] "list"
> > dim(s)
> [1] 3 3
> >
>
> Now, I'd like to remove the columns which contain NULL (it's alway the
> whole column).
> How can I do this???
>
> Antje
As long as it is always the whole column, you can just test the first row,
like this:
> s<-matrix(list(NULL,NULL,NULL,NULL,NULL,NULL,numeric(512),numeric
(512),2),ncol=3)
> s[,!apply(s,2,sapply,is.null)[1,],drop=FALSE]
[,1]
[1,] Numeric,512
[2,] Numeric,512
[3,] 2
If you would like to make sure that the whole column is NULL, you could do
something like the following:
> s[,!apply(apply(s,2,sapply,is.null),2,sum)==nrow(s),drop=FALSE]
[,1]
[1,] Numeric,512
[2,] Numeric,512
[3,] 2
I use the "drop=FALSE" here for display purposes, but you may want to leave it
off depending on desired format.
Mark
More information about the R-help
mailing list