有人知道如何从R中的data。frame中移除一整列吗?例如,如果我得到这个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
我想去掉第二列。
有几个选项可以使用dplyr::select()和一些辅助函数删除一个或多个列。helper函数可能很有用,因为有些helper函数不需要命名要删除的所有特定列。请注意,要使用select()删除列,您需要使用前导-来否定列名。
使用dplyr::starwars样本数据来获得不同的列名:
library(dplyr)
starwars %>%
select(-height) %>% # a specific column name
select(-one_of('mass', 'films')) %>% # any columns named in one_of()
select(-(name:hair_color)) %>% # the range of columns from 'name' to 'hair_color'
select(-contains('color')) %>% # any column name that contains 'color'
select(-starts_with('bi')) %>% # any column name that starts with 'bi'
select(-ends_with('er')) %>% # any column name that ends with 'er'
select(-matches('^v.+s$')) %>% # any column name matching the regex pattern
select_if(~!is.list(.)) %>% # not by column name but by data type
head(2)
# A tibble: 2 x 2
homeworld species
<chr> <chr>
1 Tatooine Human
2 Tatooine Droid
你也可以按列号下拉:
starwars %>%
select(-2, -(4:10)) # column 2 and columns 4 through 10
您可以将其设置为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
正如评论中所指出的,这里有一些其他的可能性:
Data[2] <- NULL # Wojciech Sobala
Data[[2]] <- NULL # same as above
Data <- Data[,-2] # Ian Fellows
Data <- Data[-2] # same as above
你可以通过以下方法删除多个列:
Data[1:2] <- list(NULL) # Marek
Data[1:2] <- NULL # does not work!
但是要小心矩阵子集的设置,因为你最终会得到一个向量:
Data <- Data[,-(2:3)] # vector
Data <- Data[,-(2:3),drop=FALSE] # still a data.frame