假设有三组数据:
> weight = c(150, 135, 210, 140)
> height = c(65, 61, 70, 65)
> gender = c("Fe","Fe","M","Fe")
据此,我们能够得到数据框study:
> study = data.frame(weight,height,gender)
> study
weight height gender
1 150 65 Fe
2 135 61 Fe
3 210 70 M
4 140 65 Fe
列名依次是weight、height、gender,行名依次是1、2、3、4。
关于列名,可以在构造数据框的时候指定:
> study = data.frame(w=weight,h=height,g=gender)
> study
w h g
1 150 65 Fe
2 135 61 Fe
3 210 70 M
4 140 65 Fe
也能够在后期更改:#########################列用names(数据框)<-c( )修改
>
names(study) <- c("wei","hei","gen")
> study
wei hei gen
1 150 65 Fe
2 135 61 Fe
3 210 70 M
4 140 65 Fe
行名可以用下面的方式修改:#########################行用row.names(数据框)<-c( )修改
> row.names(study)<-c("Mary","Alice","Bob","Judy")
> study
wei hei gen
Mary 150 65 Fe
Alice 135 61 Fe
Bob 210 70 M
Judy 140 65 Fe