ℙℕℤℝ 2011-06-08 23:02
浏览 722
已采纳

在 r 中从 data.frame 删除整个列

Does anyone know how to remove an entire column from a data.frame in R? For example if I am given this data.frame:

> head(data)
   chr       genome region
1 chr1 hg19_refGene    CDS
2 chr1 hg19_refGene   exon
3 chr1 hg19_refGene    CDS
4 chr1 hg19_refGene   exon
5 chr1 hg19_refGene    CDS
6 chr1 hg19_refGene   exon

and I want to remove the 2nd column.

转载于:https://stackoverflow.com/questions/6286313/remove-an-entire-column-from-a-data-frame-in-r

  • 写回答

5条回答 默认 最新

  • 斗士狗 2011-06-08 23:06
    关注

    You can set it to NULL.

    > Data$genome <- NULL
    > head(Data)
       chr region
    1 chr1    CDS
    2 chr1   exon
    3 chr1    CDS
    4 chr1   exon
    5 chr1    CDS
    6 chr1   exon
    

    As pointed out in the comments, here are some other possibilities:

    Data[2] <- NULL    # Wojciech Sobala
    Data[[2]] <- NULL  # same as above
    Data <- Data[,-2]  # Ian Fellows
    Data <- Data[-2]   # same as above
    

    You can remove multiple columns via:

    Data[1:2] <- list(NULL)  # Marek
    Data[1:2] <- NULL        # does not work!
    

    Be careful with matrix-subsetting though, as you can end up with a vector:

    Data <- Data[,-(2:3)]             # vector
    Data <- Data[,-(2:3),drop=FALSE]  # still a data.frame
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan