Applyiing mode() or class() to each column of a data.frame XXXX

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Applyiing mode() or class() to each column of a data.frame XXXX

Dan Abner
Hi everyone,

I am attempting to use the apply() function to obtain the mode and class of
each column in a data frame, however, I am encountering unexpected results.
I have the following example data:


v13<-1:6
v14<-c(1,2,3,3,NA,1)
v15<-c("Good","Bad",NA,"Good","Bad","Bad")
f4<-factor(rep(c("Blue","Red","Green"),2))
v16<-c(F,T,F,F,T,F)
data6<-data.frame(v13,v14,v15,f4,v16)
data6


Here is my function definition:


contents<-function(x){
 output<-data.frame(Varnum=1:ncol(x),
  Name=names(x),
  Mode=apply(x,2,mode),
  Class=apply(x,2,class))
 print(output)
}

====

When I call the function, I obtain the following:


> contents(data6)
    Varnum Name      Mode     Class
v13      1  v13 character character
v14      2  v14 character character
v15      3  v15 character character
  f4       4   f4 character character
v16      5  v16 character character

=====

Any help is appreciated.

Thank you,

Dan

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Applyiing mode() or class() to each column of a data.frame XXXX

mark leeds
Hi Dan: The reason that happens is because apply expects a matrix as input
so, since you sent in a dataframe ( which is really a list with each
component the same length ), apply converts the input dataframe to a matrix
but, when doing this, it needs to make conversions because a matrix needs
to be one type. ( that was a long sentence ).

So, you need to use lapply instead of apply. But when you use lapply you
need to unlist the output because lapply returns a list which causes havoc
when constructing the output dataframe. See contentsb below.

#=========================================================================

v13<-1:6
v14<-c(1,2,3,3,NA,1)
v15<-c("Good","Bad",NA,"Good","Bad","Bad")
f4<-factor(rep(c("Blue","Red","Green"),2))
v16<-c(F,T,F,F,T,F)
data6<-data.frame(v13,v14,v15,f4,v16)

contentsb<-function(x){
 output<-data.frame(Varnum=1:ncol(x),
 Name=names(x),
 Mode=unlist(lapply(x,mode)),
 Class=unlist(lapply(x,class)))
 print(output)
}

contentsb(data6)



On Thu, Dec 29, 2011 at 7:13 PM, Dan Abner <[hidden email]> wrote:

> Hi everyone,
>
> I am attempting to use the apply() function to obtain the mode and class of
> each column in a data frame, however, I am encountering unexpected results.
> I have the following example data:
>
>
> v13<-1:6
> v14<-c(1,2,3,3,NA,1)
> v15<-c("Good","Bad",NA,"Good","Bad","Bad")
> f4<-factor(rep(c("Blue","Red","Green"),2))
> v16<-c(F,T,F,F,T,F)
> data6<-data.frame(v13,v14,v15,f4,v16)
> data6
>
>
> Here is my function definition:
>
>
> contents<-function(x){
>  output<-data.frame(Varnum=1:ncol(x),
>  Name=names(x),
>  Mode=apply(x,2,mode),
>  Class=apply(x,2,class))
>  print(output)
> }
>
> ====
>
> When I call the function, I obtain the following:
>
>
> > contents(data6)
>    Varnum Name      Mode     Class
> v13      1  v13 character character
> v14      2  v14 character character
> v15      3  v15 character character
>  f4       4   f4 character character
> v16      5  v16 character character
>
> =====
>
> Any help is appreciated.
>
> Thank you,
>
> Dan
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Applyiing mode() or class() to each column of a data.frame XXXX

Adams, Jean
In reply to this post by Dan Abner
Dan Abner wrote on 12/29/2011 06:13:11 PM:

> Hi everyone,
>
> I am attempting to use the apply() function to obtain the mode and class
of
> each column in a data frame, however, I am encountering unexpected
results.

> I have the following example data:
>
>
> v13<-1:6
> v14<-c(1,2,3,3,NA,1)
> v15<-c("Good","Bad",NA,"Good","Bad","Bad")
> f4<-factor(rep(c("Blue","Red","Green"),2))
> v16<-c(F,T,F,F,T,F)
> data6<-data.frame(v13,v14,v15,f4,v16)
> data6
>
>
> Here is my function definition:
>
>
> contents<-function(x){
>  output<-data.frame(Varnum=1:ncol(x),
>   Name=names(x),
>   Mode=apply(x,2,mode),
>   Class=apply(x,2,class))
>  print(output)
> }


Use sapply() instead of apply().  In the help file for apply() it says: "
If X is not an array but an object of a class with a non-null dim value
(such as a data frame), apply attempts to coerce it to an array via
as.matrix if it is two-dimensional (e.g., a data frame) or via as.array."
This coercion to a matrix might be causing the unexpected result. sapply()
and lapply() are designed specifically for lists (which a data frame is).
I also simplified the function a bit ...

contents<-function(x){
        data.frame(Varnum=1:ncol(x), Name=names(x),
        Mode=sapply(x,mode), Class=sapply(x,class))
        }

Jean


> ====
>
> When I call the function, I obtain the following:
>
>
> > contents(data6)
>     Varnum Name      Mode     Class
> v13      1  v13 character character
> v14      2  v14 character character
> v15      3  v15 character character
>   f4       4   f4 character character
> v16      5  v16 character character
>
> =====
>
> Any help is appreciated.
>
> Thank you,
>
> Dan
>
>    [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Applyiing mode() or class() to each column of a data.frame XXXX

Justin Haynes
there is also colwise in the plyr package.

> library(plyr)
> colwise(class)(data6)
      v13     v14       v15     f4     v16
1 integer numeric character factor logical


Justin


On Thu, Dec 29, 2011 at 4:47 PM, Jean V Adams <[hidden email]> wrote:

> Dan Abner wrote on 12/29/2011 06:13:11 PM:
>
> > Hi everyone,
> >
> > I am attempting to use the apply() function to obtain the mode and class
> of
> > each column in a data frame, however, I am encountering unexpected
> results.
> > I have the following example data:
> >
> >
> > v13<-1:6
> > v14<-c(1,2,3,3,NA,1)
> > v15<-c("Good","Bad",NA,"Good","Bad","Bad")
> > f4<-factor(rep(c("Blue","Red","Green"),2))
> > v16<-c(F,T,F,F,T,F)
> > data6<-data.frame(v13,v14,v15,f4,v16)
> > data6
> >
> >
> > Here is my function definition:
> >
> >
> > contents<-function(x){
> >  output<-data.frame(Varnum=1:ncol(x),
> >   Name=names(x),
> >   Mode=apply(x,2,mode),
> >   Class=apply(x,2,class))
> >  print(output)
> > }
>
>
> Use sapply() instead of apply().  In the help file for apply() it says: "
> If X is not an array but an object of a class with a non-null dim value
> (such as a data frame), apply attempts to coerce it to an array via
> as.matrix if it is two-dimensional (e.g., a data frame) or via as.array."
> This coercion to a matrix might be causing the unexpected result. sapply()
> and lapply() are designed specifically for lists (which a data frame is).
> I also simplified the function a bit ...
>
> contents<-function(x){
>        data.frame(Varnum=1:ncol(x), Name=names(x),
>        Mode=sapply(x,mode), Class=sapply(x,class))
>        }
>
> Jean
>
>
> > ====
> >
> > When I call the function, I obtain the following:
> >
> >
> > > contents(data6)
> >     Varnum Name      Mode     Class
> > v13      1  v13 character character
> > v14      2  v14 character character
> > v15      3  v15 character character
> >   f4       4   f4 character character
> > v16      5  v16 character character
> >
> > =====
> >
> > Any help is appreciated.
> >
> > Thank you,
> >
> > Dan
> >
> >    [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] 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]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Applyiing mode() or class() to each column of a data.frame XXXX

Hadley Wickham-2
But be careful because class is a character vector (not necessarily a
character vector of length 1)

On Fri, Dec 30, 2011 at 10:21 AM, Justin Haynes <[hidden email]> wrote:

> there is also colwise in the plyr package.
>
>> library(plyr)
>> colwise(class)(data6)
>      v13     v14       v15     f4     v16
> 1 integer numeric character factor logical
>
>
> Justin
>
>
> On Thu, Dec 29, 2011 at 4:47 PM, Jean V Adams <[hidden email]> wrote:
>
>> Dan Abner wrote on 12/29/2011 06:13:11 PM:
>>
>> > Hi everyone,
>> >
>> > I am attempting to use the apply() function to obtain the mode and class
>> of
>> > each column in a data frame, however, I am encountering unexpected
>> results.
>> > I have the following example data:
>> >
>> >
>> > v13<-1:6
>> > v14<-c(1,2,3,3,NA,1)
>> > v15<-c("Good","Bad",NA,"Good","Bad","Bad")
>> > f4<-factor(rep(c("Blue","Red","Green"),2))
>> > v16<-c(F,T,F,F,T,F)
>> > data6<-data.frame(v13,v14,v15,f4,v16)
>> > data6
>> >
>> >
>> > Here is my function definition:
>> >
>> >
>> > contents<-function(x){
>> >  output<-data.frame(Varnum=1:ncol(x),
>> >   Name=names(x),
>> >   Mode=apply(x,2,mode),
>> >   Class=apply(x,2,class))
>> >  print(output)
>> > }
>>
>>
>> Use sapply() instead of apply().  In the help file for apply() it says: "
>> If X is not an array but an object of a class with a non-null dim value
>> (such as a data frame), apply attempts to coerce it to an array via
>> as.matrix if it is two-dimensional (e.g., a data frame) or via as.array."
>> This coercion to a matrix might be causing the unexpected result. sapply()
>> and lapply() are designed specifically for lists (which a data frame is).
>> I also simplified the function a bit ...
>>
>> contents<-function(x){
>>        data.frame(Varnum=1:ncol(x), Name=names(x),
>>        Mode=sapply(x,mode), Class=sapply(x,class))
>>        }
>>
>> Jean
>>
>>
>> > ====
>> >
>> > When I call the function, I obtain the following:
>> >
>> >
>> > > contents(data6)
>> >     Varnum Name      Mode     Class
>> > v13      1  v13 character character
>> > v14      2  v14 character character
>> > v15      3  v15 character character
>> >   f4       4   f4 character character
>> > v16      5  v16 character character
>> >
>> > =====
>> >
>> > Any help is appreciated.
>> >
>> > Thank you,
>> >
>> > Dan
>> >
>> >    [[alternative HTML version deleted]]
>> >
>> > ______________________________________________
>> > [hidden email] 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]]
>>
>> ______________________________________________
>> [hidden email] 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]]
>
> ______________________________________________
> [hidden email] 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.



--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

______________________________________________
[hidden email] 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.