r - 如何找到具有相同值的连续三行

标签 r

我有一个数据框如下:

chr     leftPos    Sample1  X.DD   3_samples    MyStuff
1        324         -1        1        1           1
1        4565        -1        0        0           0 
1        6887        -1        1        0           0
1        12098        1       -1        1           1
2        12          -1        1        0           1
2        43          -1        1        1           1
5        1           -1        1        1           0
5        43           0        1       -1           0
5        6554         1        1        1           1
5        7654        -1        0        0           0
5        8765         1        1        1           0
5        9833         1        1        1          -1
6        12           1        1        0           0
6        43           0        0        0           0
6        56           1        0        0           0
6        79           1        0       -1           0
6        767          1        0       -1           0
6        3233         1        0       -1           0

我想按照以下规则进行转换
对于每条染色体:

一种。如果一列中连续出现三个或更多 1 或 -1,则该值保持原样。

湾如果一列中连续的 1 或 -1 少于三个,则 1 或 -1 的值更改为 0

列中的行必须具有相同的符号(+ 或 -ve)才能被称为连续。

上面数据框的结果应该是:
chr     leftPos    Sample1  X.DD   3_samples    MyStuff
    1        324         -1        0        0           0
    1        4565        -1        0        0           0 
    1        6887        -1        0        0           0
    1        12098        0        0        0           0
    2        12           0        0        0           0
    2        43           0        0        0           0
    5        1            0        1        0           0
    5        43           0        1        0           0
    5        6554         0        1        0           0
    5        7654         0        0        0           0
    5        8765         0        0        0           0
    5        9833         0        0        0           0
    6        12           0        0        0           0
    6        43           0        0        0           0
    6        56           1        0        0           0
    6        79           1        0       -1           0
    6        767          1        0       -1           0
    6        3233         1        0       -1           0

我已经设法对连续两行执行此操作,但我不确定如何为三行或更多行更改此设置。
DAT_list2res <-cbind(DAT_list2[1:2],DAT_list2res)
colnames(DAT_list2res)[1:2]<-c("chr","leftPos")
DAT_list2res$chr<-as.numeric(gsub("chr","",DAT_list2res$chr))
DAT_list2res<-as.data.frame(DAT_list2res)
dx<-DAT_list2res
f0 <- function( colNr, dx)
{
  col <- dx[,colNr]
  n1 <- which(col == 1| col == -1)          # The `1`-rows.
  d0 <- which( diff(col) == 0)      # Consecutive rows in a column are equal.
  dc0 <- which( diff(dx[,1]) == 0)  # Same chromosome.
  m <- intersect( n1-1, intersect( d0, dc0 ) )
  return ( setdiff( 1:nrow(dx), union(m,m+1) ) )
}
g <- function( dx )
{
  for ( i in 3:ncol(dx) ) { dx[f0(i,dx),i] <- 0 }  
  return ( dx )
}
dx<-g(dx)

最佳答案

这是仅使用基本 R 的一种解决方案。

首先定义一个函数,它将替换小于 3 的任何重复为零:

replace_f <- function(x){
  subs <- rle(x)
  subs$values[subs$lengths < 3] <- 0
  inverse.rle(subs)
}

然后将您的 data.framechr 拆分,然后将该函数应用于您想要更改的所有列(在本例中为第 3 到 6 列):
df[,3:6] <- do.call("rbind", lapply(split(df[,3:6], df$chr), function(x) apply(x, 2, replace_f)))

请注意,在替换原始数据之前,我们将结果与 rbind 结合在一起。这会给你想要的结果:
   chr leftPos Sample1 X.DD X3_samples MyStuff
1    1     324      -1    0          0       0
2    1    4565      -1    0          0       0
3    1    6887      -1    0          0       0
4    1   12098       0    0          0       0
5    2      12       0    0          0       0
6    2      43       0    0          0       0
7    5       1       0    1          0       0
8    5      43       0    1          0       0
9    5    6554       0    1          0       0
10   5    7654       0    0          0       0
11   5    8765       0    0          0       0
12   5    9833       0    0          0       0
13   6      12       0    0          0       0
14   6      43       0    0          0       0
15   6      56       1    0          0       0
16   6      79       1    0         -1       0
17   6     767       1    0         -1       0
18   6    3233       1    0         -1       0

关于r - 如何找到具有相同值的连续三行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34250325/

相关文章:

r - 包含字符串和数字的聚合数据框

r - geom_text_repel 的控制颜色

R 谷歌表 : Unable to use `gs_auth()` in googlesheets package - Sign In With Google Temporarily Disabled App Not Verified Issue

linux - 如何在 Linux Mint 17.1 上安装 R 3.1.2

R:避免summary.plm

python - 对于线性模型,与 R 预测函数等效的 Python 是什么?

r - Windows 下 R 图形中的抗锯齿(根据 Mac)

r - ggplot2:将边距之一保留为默认值

r - 解释 hclust/heatmap.2 中 'cutree' 的结果

r - R : Error in svd(x, nu=0, nv=k) 中的 PCA: 'x' 中的无限值或缺失值