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]]
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]]
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