in R
Determine the class of each column in the flights data set from the nycflights13 package.
##to load nycflights13 dataset
library(nycflights13)
## to display
flights
##---------------------------------------------------------------------------------
##way to find out class of particular column 'hour' in dataframe
flight
class(flights$hour)
#ans is
> class(flights$hour)
[1] "numeric"
##---------------------------------------------------------------------------------
## one way to find classes of flights dataset
str(flights)
#answer is as follows
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 336776 obs.
of 19 variables:
$ year : int 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013
...
$ month : int 1 1 1 1 1 1 1 1 1 1 ...
$ day : int 1 1 1 1 1 1 1 1 1 1 ...
$ dep_time : int 517 533 542 544 554 554 555 557 557 558 ...
$ sched_dep_time: int 515 529 540 545 600 558 600 600 600 600
...
$ dep_delay : num 2 4 2 -1 -6 -4 -5 -3 -3 -2 ...
$ arr_time : int 830 850 923 1004 812 740 913 709 838 753 ...
$ sched_arr_time: int 819 830 850 1022 837 728 854 723 846 745
...
$ arr_delay : num 11 20 33 -18 -25 12 19 -14 -8 8 ...
$ carrier : chr "UA" "UA" "AA" "B6" ...
$ flight : int 1545 1714 1141 725 461 1696 507 5708 79 301
...
$ tailnum : chr "N14228" "N24211" "N619AA" "N804JB" ...
$ origin : chr "EWR" "LGA" "JFK" "JFK" ...
$ dest : chr "IAH" "IAH" "MIA" "BQN" ...
$ air_time : num 227 227 160 183 116 150 158 53 140 138 ...
$ distance : num 1400 1416 1089 1576 762 ...
$ hour : num 5 5 5 5 6 5 6 6 6 6 ...
$ minute : num 15 29 40 45 0 58 0 0 0 0 ...
$ time_hour : POSIXct, format: "2013-01-01 05:00:00" "2013-01-01
05:00:00" "2013-01-01 05:00:00" ...
##---------------------------------------------------------------------------------
##---------------------------------------------------------------------------------
##another way using lapply to apply class function on every
column
lapply(flights,class)
## answer
> lapply(flights,class)
$year
[1] "integer"
$month
[1] "integer"
$day
[1] "integer"
$dep_time
[1] "integer"
$sched_dep_time
[1] "integer"
$dep_delay
[1] "numeric"
$arr_time
[1] "integer"
$sched_arr_time
[1] "integer"
$arr_delay
[1] "numeric"
$carrier
[1] "character"
$flight
[1] "integer"
$tailnum
[1] "character"
$origin
[1] "character"
$dest
[1] "character"
$air_time
[1] "numeric"
$distance
[1] "numeric"
$hour
[1] "numeric"
$minute
[1] "numeric"
$time_hour
[1] "POSIXct" "POSIXt"
Get Answers For Free
Most questions answered within 1 hours.