Subsetting: Data Frames
> DATA FRAMES
(if you only have 1 var in the data frame, treated like a list--with 2 or more vectors, df behaves like matrix)
Taken directly from site: Â How many of your friends complain regularly about their bodies? ~None: 147 ~A few: 411 ~Some: 188 ~Most: 141 ~All: 53
construct an object that reflects it:
> body_complain<-data.frame(x=c('None','A few','Some','Most','All'),y=c(147,411,188,141,53))
object $ title of column
___________________________________________________________
>DATA FRAMES>STRUCTURAL QUALITIES
the structure of data frame subsets are uniqueÂ
examine the differences using this variable (a)
> a  x y z 1 1 1 a 2 2 2 b 3 3 3 c 4 4 4 d
> str(a) 'data.frame':  4 obs. of  3 variables: $ x: int  1 2 3 4 $ y: int  1 2 3 4 $ z: Factor w/ 4 levels "a","b","c","d": 1 2 3 4
Now examine the structural differences which subsetting initiates in data frames
> str(a[c(1)]) 'data.frame':  4 obs. of  1 variable: $ x: int  1 2 3 4
> str(a[,c(1,3)]) 'data.frame':  4 obs. of  2 variables: $ x: int  1 2 3 4 $ z: Factor w/ 4 levels "a","b","c","d": 1 2 3 4
> str(a[c(3),]) 'data.frame':  1 obs. of  3 variables: $ x: int 3 $ y: int 3 $ z: Factor w/ 4 levels "a","b","c","d": 3
To access a subscript of a data frame use $ and []
>a$y[3]
[1] 3
__________________________________________________________
>DATA FRAME > REASSIGNMENT
reassigning data frame elements combines subsets and reassignment and is discussed further in the section: Subsetting and Assignment












