使用R删除大型数据集中的一些特殊列

时间:2014-02-09 19:50:17

标签: r

我使用大数据集(1200 * 10000),在我的数据集中,一些列具有相同的值,除了一两点,我需要检测并删除此列,例如在“1846”列中:< / p>

> x[317:400,1846]

 [1] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2

[81] 2 2 **1** 2

其他行值(1:317和400:1200)= 2.

我该如何解决这个问题?

例如,在我的文件的某些部分(1200 * 10000),

x
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
 [1,]    1    1    0    1    2    0    1    0    1     2     2     1
 [2,]    1    1    0    1    2    0    1    0    1     2     1     1
 [3,]    2    1    0    1    2    0    1    0    1     2     2     1
 [4,]    1    2    0    1    2    0    1    0    1     2     2     2
 [5,]    0    1    0    1    2    0    1    0    1     2     1     1
 [6,]    2    0    0    1    2    0    1    2    0     2     1     2
 [7,]    1    1    0    1    2    1    1    0    1     2     0     2
 [8,]    0    1    0    1    2    0    1    0    1     2     0     0
 [9,]    0    1    0    1    2    0    1    0    1     1     2     1
[10,]    1    1    0    1    2    0    1    0    1     2     1     1

我想删除原始数据集列,例如3到10。

8 个答案:

答案 0 :(得分:1)

在第一篇文章中继续我的回答,

detect.col <- function(
  x,
  n.diff=3 # the minimal  number of unique values required per column
  )
{
  ret <- which(apply(x,2,function(e){length(unique(e))}) >= n.diff)
  ret  
}

x[,detect.col(x)]

我想这就是你的意思?

答案 1 :(得分:1)

mm<-read.table(text="      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
 [1,]    1    1    0    1    2    0    1    0    1     2     2     1
 [2,]    1    1    0    1    2    0    1    0    1     2     1     1
 [3,]    2    1    0    1    2    0    1    0    1     2     2     1
 [4,]    1    2    0    1    2    0    1    0    1     2     2     2
 [5,]    0    1    0    1    2    0    1    0    1     2     1     1
 [6,]    2    0    0    1    2    0    1    2    0     2     1     2
 [7,]    1    1    0    1    2    1    1    0    1     2     0     2
 [8,]    0    1    0    1    2    0    1    0    1     2     0     0
 [9,]    0    1    0    1    2    0    1    0    1     1     2     1
[10,]    1    1    0    1    2    0    1    0    1     2     1     1", row.names=1, header=T)

现在,

mm[,which(apply(mm,2,function (x) {length(unique(x))})==3)

输出

      X..1. X..2. X..11. X..12.
[1,]      1     1      2      1
[2,]      1     1      1      1
[3,]      2     1      2      1
[4,]      1     2      2      2
[5,]      0     1      1      1
[6,]      2     0      1      2
[7,]      1     1      0      2
[8,]      0     1      0      0
[9,]      0     1      2      1
[10,]     1     1      1      1

答案 2 :(得分:0)

假设您的data.frame名为x,这将只保留具有一个不同值的列:

keepIndex <- apply(
    x, 
    2, 
    FUN = function(column) {
        return(length(unique(column)) == 1)
    })

x <- x[, keepIndex]

答案 3 :(得分:0)

这应该有用,

m<-matrix(2,nrow=100, ncol=100)  #making dummy matrix m  
m[sample(1:100,10), sample(1:100,10)]<-1       #replacing some random row and col to 1
m[,-which(colSums(m==1)>0)]     #getting rid of cols with 1

答案 4 :(得分:0)

基于布尔索引的解决方案。

> x<-cbind(c(1,1,1,1),c(1,1,1,2),c(1,1,1,1))
> x
     [,1] [,2] [,3]
[1,]    1    1    1
[2,]    1    1    1
[3,]    1    1    1
[4,]    1    2    1
> x[,colSums(x!=x[1,])==0]
     [,1] [,2]
[1,]    1    1
[2,]    1    1
[3,]    1    1
[4,]    1    1

答案 5 :(得分:0)

如果您的数据存储在名为df的数据框中:

df[ ,sapply(df, function(x) all(x[1] == x[-1]))]

答案 6 :(得分:0)

搜索整个数据或其中的一部分:

detect.col <- function(
  x,row.from=1,row.to=nrow(x),col.from=1,col.to=ncol(x),
  n.diff=3 # the minimal number of unique values required per column
  )
{
  tmp.x <- x[row.from:row.to,col.from:col.to]
  ret <- which(apply(tmp.x,2,function(e){length(unique(e))}) < n.diff )
  if(length(ret)){
    ret <- ret+col.from-1
  }
  ret  
}

## search the whole
detect.col(x) # columns to remove

## Or only search within a range, like in your case
row.from <- 317
row.to <- 400

col.from <- 1000
col.to <- 2000

col.to.remove <- detect.col(x,row.from,row.to,col.from,col.to)
x[,-col.to.remove] # print those to keep

答案 7 :(得分:0)

我不确定,但我认为您要删除n-1n-2行中包含单个值的任何列,其中n是行数。如果是这样,那么你想要删除:

<{1}}中的

x2,因为它包含9'1和1'0'和

<{1}}中的

my.data,因为它包含8个2和2个'。

以下代码就是这样做的。对不起,如果这不是你想要做的。我不确定这段代码是否能在庞大的数据框架中表现良好。

x5