2

私のデータは、ヘッダーなしで、必ずしも同じ種類の情報を含むとは限らない列を使用して、特定の方法で配置されています。その一部は、次を使用して作成できます。

data <- textConnection("rs123,22,337647,C,T
1,7385,0.4156,-0.0019,0.0037
1,16550,0.959163800640972,-0.0241,0.0128
1,17218,0.0528,0.015,0.039
rs193,22,366349,C,T
1,7385,0.3708,0.0017,0.0035
1,16550,0.793259111116741,-0.0028,0.009
1,17218,0.9547,-0.016,0.033
rs194,22,366300,NA,NA
0,0,0,0,0
0,0,0,0,0
0,0,0,0,0
rs118,22,301327,C,T
1,7385,0.0431,-0.0085,0.0077
1,16550,0.789981059331214,0.0036,0.0092
1,17218,0.99,-0.057,0.062
rs120,22,497528,C,G
1,7385,0.0716,0.0012,0.0073
1,16550,0.233548238634496,-0.0033,0.0064
1,17218,0.4563,-0.002,0.015
rs109,22,309825,A,G
1,5520,0.8611,2e-04,0.0044
0,0,0,0,0
1,17218,0.9762,0.076,0.044
rs144,22,490068,C,T
0,0,0,0,0
0,0,0,0,0
1,17218,0.2052,-0.013,0.032")
mydata <- read.csv(data, header = F, sep = ",", stringsAsFactors=FALSE)

私の質問は次のとおりです。「NA」を含む行を grep/awk 行に書き込むことができます (これらはデータを含まない SNP です)。

grep -v 'NA' file.in > file.out

しかし、次の3行も削除するように指定するにはどうすればよいですか? すべてゼロを含むすべての行を削除したくはありません。「NA」を含む SNP を含む行に続くすべてゼロを含む行のみを削除します。

ご意見ありがとうございます。

4

4 に答える 4

3

使用GNU sed(アドレスに続く行数は内線番号であるため):

sed -e '/NA/,+3 d' infile

編集awkしてソリューションを追加します。

awk '/NA/ { for ( i = 1; i <= 4; i++ ) { getline; } } { print }' infile
于 2012-10-12T20:07:24.917 に答える
1

更新:私の以前の答えはおそらく間違っていたので、この代替手段があります:

nas <- apply(mydata, 1, function(x) any(is.na(x)))
s <- apply(mydata == 0, 1, all)
out <- which(nas)
for (i in which(nas)) {
  j <- i + 1
  while (!is.na(s[j]) && s[j]) {
    out <- c(out, j)
    j <- j + 1
  }
}
mydata2 <- mydata[-out,]

最初は、NA の後の最初の 3 行だけを気にしていると思っていましたが、実際には、各 NA の後にすべてゼロの連続する行をすべて削除したいようです。

(これは私の以前の答えです:)

nas <- apply(mydata, 1, function(x) any(is.na(x)))
whereToLook <- sort(which(nas) + 1:3)
s <- apply(mydata == 0, 1, prod)
zeros <- which(s == 1)
whereToErase <- zeros[zeros %in% whereToLook]
whereToErase <- c(which(nas), whereToErase)
于 2012-10-12T20:21:41.170 に答える
1

R にインポートした後、次のことができます。

# identify the rows containing any NA's
narows <- which(apply(mydata,1,function(x) any(is.na(x))))
# identify the rows containing all 0's
zerorows <- which(apply(mydata==0,1,all))

# get the rows that either contain NAs, or are all 0 and are 
# within 3 of the NA rows
rowstodelete <- c(narows,
                  intersect(
                    (sapply(c(narows),function(x) seq(x,x+3))),
                    zerorows
                  )
                )

# subset mydata to only remove the NA rows + the following 3 "zero rows"
mydata[-rowstodelete,]
于 2012-10-12T20:37:59.777 に答える
0

これはあなたのために働くかもしれません(GNU sed):

 sed '/\<NA\>/!b;:a;$!N;s/\n\(0,\)\+0$//;ta;D' file

NAこれにより、次の0,...0行を含むすべての行が削除されます。

于 2012-10-13T06:52:29.297 に答える