删除名为“NA"的列 [英] Removing Columns Named "NA"

查看:44
本文介绍了删除名为“NA"的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一些 RNA-seq 计数数据,我有大约 60,000 列包含基因名称和 24 行包含样本名称.当我进行一些基因名称转换时,我留下了一堆名为 NA 的列.我知道 R 处理 NA 与典型的列名不同,我的问题是如何删除这些列.这是我的数据示例.

I'm dealing with some RNA-seq count data for which I have ~60,000 columns containing gene names and 24 rows containing sample names. When I did some gene name conversions I was left with a bunch of columns that are named NA. I know that R handles NA differently than a typical column name and my question is how do I remove these columns. Here is an example of my data.

  "Gene1"  "Gene2"  "Gene3"  NA  "Gene4"
1  10       11       12      10   15
2  13       12       50      40   30
3  34       23       23      21   22

我希望它最终像

  "Gene1"  "Gene2"  "Gene3"  "Gene4"
1  10       11       12       15
2  13       12       50       30
3  34       23       23       22

我确实确定了一些对其他人有效但对我无效的 R 代码

I did identify some R code that worked for others but not for me

df<-df[, grep("^(NA)", names(df), value = TRUE, invert = TRUE)]

推荐答案

看起来您的名字中有实际的 NA,而不是 "NA".前者代表一个缺失值,后者是一个字符串,看起来像代表缺失值的符号.使用:

Looks like you have an actual NA in your names, instead of "NA". The former represents a missing value, the latter is a character string that looks like the symbol that represents the missing value. Use:

df <- df[!is.na(names(df))]

iris来说明:

> head(iris)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa
> names(iris)[2:3] <- NA
> head(iris)
  Sepal.Length  NA  NA Petal.Width Species
1          5.1 3.5 1.4         0.2  setosa
2          4.9 3.0 1.4         0.2  setosa
3          4.7 3.2 1.3         0.2  setosa
4          4.6 3.1 1.5         0.2  setosa
5          5.0 3.6 1.4         0.2  setosa
6          5.4 3.9 1.7         0.4  setosa
> head(iris[!is.na(names(iris))])
  Sepal.Length Petal.Width Species
1          5.1         0.2  setosa
2          4.9         0.2  setosa
3          4.7         0.2  setosa
4          4.6         0.2  setosa
5          5.0         0.2  setosa
6          5.4         0.4  setosa

这篇关于删除名为“NA"的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆