[R] apply problem
Prof Brian Ripley
ripley at stats.ox.ac.uk
Fri Apr 13 20:10:53 CEST 2007
On Fri, 13 Apr 2007, aedin culhane wrote:
> Dear R-Help
> I am running apply on a data.frame containing factors and numeric
> columns. It appears to convert are columns into as.character? Does it
> convert data.frame into matrix? Is this expected? I wish it to recognise
Yes, and quite explicit on the help page
Arguments:
X: the array to be used.
^^^^^
If 'X' is not an array but has a dimension attribute, 'apply'
attempts to coerce it to an array via 'as.matrix' if it is
two-dimensional (e.g., data frames) or via 'as.array'.
I am baffled as to how you managed to miss this, as it is part of the
homework the posting guide asked you to do *before* posting.
> numerical columns and round numbers. Can I use another function instead
> of apply, or should I use a for loop in the case?
You haven;t told us _what_ you want to do, but lapply works by column.
> > summary(xmat)
> A B C D
> Min. : 1.0 414 : 1 Stage 2: 5 Min. :-0.075369
> 1st Qu.:113.8 422 : 1 Stage 3: 6 1st Qu.:-0.018102
> Median :226.5 426 : 1 Stage 4:441 Median :-0.003033
> Mean :226.5 436 : 1 Mean : 0.008007
> 3rd Qu.:339.2 460 : 1 3rd Qu.: 0.015499
> Max. :452.0 462 : 1 Max. : 0.400578
> (Other):446
> E F G
> Min. :0.2345 Min. :0.9808 Min. :0.01558
> 1st Qu.:0.2840 1st Qu.:0.9899 1st Qu.:0.02352
> Median :0.3265 Median :0.9965 Median :0.02966
> Mean :0.3690 Mean :1.0079 Mean :0.03580
> 3rd Qu.:0.3859 3rd Qu.:1.0129 3rd Qu.:0.03980
> Max. :2.0422 Max. :1.3742 Max. :0.20062
>
> > for(i in 1:7) print(class(xmat[,i]))
> [1] "integer"
> [1] "factor"
> [1] "factor"
> [1] "numeric"
> [1] "numeric"
> [1] "numeric"
> [1] "numeric"
Better, sapply(xmat, class).
> > apply(xmat, 2, class)
> A B C D E F
> "character" "character" "character" "character" "character" "character"
> G
> "character"
Well, all columns of a matrix are of the same class.
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list