lrony* 2011-06-08 23:02 采纳率: 0%
浏览 2898
已采纳

在 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条回答 默认 最新

  • hurriedly% 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条)

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程