繁体   English   中英

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

[英]removing some special columns in large data set with R

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

> 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列。

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

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)]

我想这就是你的意思?

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

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

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

x <- x[, keepIndex]

这应该工作,

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

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

> 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

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

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

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

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

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

my.data x2列,因为它包含9个“ 1”和一个“ 0”,并且

my.data x5列,因为它包含8个2和两个1。

下面的代码可以做到这一点。 抱歉,这不是您要尝试的操作。 我不确定此代码在庞大的数据帧中是否能很好地执行。

my.data <- read.table(text='

x1  x2  x3  x4  x5  x6
 1   1   2   2   2   1
 1   1   2   1   1   2
 1   1   2   2   2   3
 1   1   2   2   2   4
 1   1   2   1   2   5
 1   1   2   2   2   6
 1   0   2   2   2   7
 1   1   2   1   2   8
 1   1   2   2   1   9
 1   1   2   2   2  10

', header = TRUE)

my.data

my.summary <- as.data.frame.matrix(table( rep(colnames(my.data), 
                      each=nrow(my.data)), unlist(my.data)))
my.summary

delete.these <- which(my.summary == (nrow(my.data)-2) | 
                      my.summary == (nrow(my.data)-1), arr.ind = TRUE)[,1]

my.data[,-delete.these]

   x1 x3 x4 x6
1   1  2  2  1
2   1  2  1  2
3   1  2  2  3
4   1  2  2  4
5   1  2  1  5
6   1  2  2  6
7   1  2  2  7
8   1  2  1  8
9   1  2  2  9
10  1  2  2 10

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM