[R] problem with using apply for dataframe

Eik Vettorazzi E.Vettorazzi at uke.de
Thu Jul 19 12:57:13 CEST 2012


Hi Marion,
as stated in the help file ?apply coerces a data.frame object into an
array. Since an array has only one type of data, this coercion turns all
your variables into strings (because this data type can hold all
information given without loss).

If it happens that your data.frame consists only of numeric variables,
then this type suffices for the array as well, and things like sum or
mean can be applied row/columnwise.

btw:

apply(mydataframe,2,function(x)sum(x))

doesn't work either (for the same reason).

Cheers

Am 19.07.2012 11:48, schrieb Marion Wenty:
> Dear people,
> 
> I am including an example of a dataframe:
> 
> mydataframe<-data.frame(X=c(1:4),total_bill=c(16.99,10.34,21.01,23.68),tip=c(1.01,1.66,3.50,3.31),sex=c("Male","Male","Male","Female"))
> 
> When I use the sapply function getting the information about the factors
> works:
> 
> sapply(mydataframe,function(x)is.factor(x))
> 
>          X total_bill        tip        sex
>      FALSE      FALSE      FALSE       TRUE
> 
> 
> But if I use the apply function it doesn't work:
> 
> apply(mydataframe,2,function(x)is.factor(x))
> 
>          X total_bill        tip        sex
>      FALSE      FALSE      FALSE      FALSE
> 
> 
> I don't understand why, because I have used the apply function for
> dataframes before e.g. using sum(x) instead of is.factor(x) and it did work.
> 
> Could anyone help me with this?
> 
> Thank you very much for your help in advance!
> 
> Marion
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.
> 


-- 
Eik Vettorazzi

Department of Medical Biometry and Epidemiology
University Medical Center Hamburg-Eppendorf

Martinistr. 52
20246 Hamburg

T ++49/40/7410-58243
F ++49/40/7410-57790

--
Pflichtangaben gemäß Gesetz über elektronische Handelsregister und Genossenschaftsregister sowie das Unternehmensregister (EHUG):

Universitätsklinikum Hamburg-Eppendorf; Körperschaft des öffentlichen Rechts; Gerichtsstand: Hamburg

Vorstandsmitglieder: Prof. Dr. Guido Sauter (Vertreter des Vorsitzenden), Dr. Alexander Kirstein, Joachim Prölß, Prof. Dr. Dr. Uwe Koch-Gromus 



More information about the R-help mailing list