ℙℕℤℝ 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条)

报告相同问题?

悬赏问题

  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂