apply(mydataframe,2,function(x){ print(x);is.factor(x)})
[1] "1" "2" "3" "4"
[1] "16.99" "10.34" "21.01" "23.68"
[1] "1.01" "1.66" "3.50" "3.31"
[1] "Male"   "Male"   "Male"   "Female"
         X total_bill        tip        sex 
     FALSE      FALSE      FALSE      FALSE 

> sapply(mydataframe,function(x){ print(x);is.factor(x)})
[1] 1 2 3 4
[1] 16.99 10.34 21.01 23.68
[1] 1.01 1.66 3.50 3.31
[1] Male   Male   Male   Female
Levels: Female Male
         X total_bill        tip        sex 
     FALSE      FALSE      FALSE       TRUE 

Seems like sapply converts the stuff and apply keeps the strings...

> 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@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.


        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to