R 从数据框中读取数据 数据结构变化[‘‘]$

> library(survival)  # 加载 survival包
Warning message:
程辑包‘survival’是用R版本4.0.5 来建造的 
> data(colon)        # 导入内置数据集
Warning message:
In data(colon) : 没有‘colon’这个数据集
> colon <- colon[1:11, 1:11] # 选取部分数据进行演示
> colon
   id study      rx sex age obstruct perfor adhere nodes
1   1     1 Lev+5FU   1  43        0      0      0     5
2   1     1 Lev+5FU   1  43        0      0      0     5
3   2     1 Lev+5FU   1  63        0      0      0     1
4   2     1 Lev+5FU   1  63        0      0      0     1
5   3     1     Obs   0  71        0      0      1     7
6   3     1     Obs   0  71        0      0      1     7
7   4     1 Lev+5FU   0  66        1      0      0     6
8   4     1 Lev+5FU   0  66        1      0      0     6
9   5     1     Obs   1  69        0      0      0    22
10  5     1     Obs   1  69        0      0      0    22
11  6     1 Lev+5FU   0  57        0      0      0     9
   status differ
1       1      2
2       1      2
3       0      2
4       0      2
5       1      2
6       1      2
7       1      2
8       1      2
9       1      2
10      1      2
11      1      2
> View(colon)         # 预览整个数据集
> names(colon)        # 输出数据框变量名称
 [1] "id"       "study"    "rx"       "sex"     
 [5] "age"      "obstruct" "perfor"   "adhere"  
 [9] "nodes"    "status"   "differ"  
> dput(names(colon))  # 输出数据框变量名称,并用c()括起来
c("id", "study", "rx", "sex", "age", "obstruct", "perfor", "adhere", 
"nodes", "status", "differ")
> class(colon$sex)                  # 查看colon数据集中sex变量的类型
[1] "numeric"
> colon$sex <- as.factor(colon$sex) # 将colon数据集中sex变量转化为因子
> is.factor(colon$sex)              # 查看colon数据集中sex变量是否为因子型变量
[1] TRUE
> class(colon[sex])
[1] "data.frame"
> colon$sex
 [1] 1 1 1 1 0 0 0 0 1 1 0
Levels: 0 1
> class(colon$sex)
[1] "factor"
> class(colon[sex])
[1] "data.frame"
> colon[sex]
   id study id.1 id.2
1   1     1    1    1
2   1     1    1    1
3   2     1    2    2
4   2     1    2    2
5   3     1    3    3
6   3     1    3    3
7   4     1    4    4
8   4     1    4    4
9   5     1    5    5
10  5     1    5    5
11  6     1    6    6
> View(colon)
> colon["sex"]
   sex
1    1
2    1
3    1
4    1
5    0
6    0
7    0
8    0
9    1
10   1
11   0
> colon[,sex]
   id study id.1 id.2
1   1     1    1    1
2   1     1    1    1
3   2     1    2    2
4   2     1    2    2
5   3     1    3    3
6   3     1    3    3
7   4     1    4    4
8   4     1    4    4
9   5     1    5    5
10  5     1    5    5
11  6     1    6    6
> colon[sex,]
    id study      rx sex age obstruct perfor adhere
1    1     1 Lev+5FU   1  43        0      0      0
2    1     1 Lev+5FU   1  43        0      0      0
1.1  1     1 Lev+5FU   1  43        0      0      0
1.2  1     1 Lev+5FU   1  43        0      0      0
    nodes status differ
1       5      1      2
2       5      1      2
1.1     5      1      2
1.2     5      1      2
> sex
[1] male   female male   male  
Levels: male female
> class(sex)
[1] "factor"
> class(colon['sex'])
[1] "data.frame"

注意:
①数据框data.frame下使用$读取数据仅读取对应列数据,并以数据原本类型进行储存(如:因子),不读出标签名
②如果使用[]读取则需要在标签名左右添加引号,否则将标签名视作变量。在示例中sex是环境中已经存在的一个变量,数据类型为因子factor,暂未知为何因子也可读出数据且难以理解读出内容逻辑
③使用[]并添加引号后,读取的则为包括标签名的那一列数据框data.frame,数据格式为data.frame

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值