在'NA'之后删除文件的行

时间:2012-10-12 19:57:47

标签: r sed awk grep

我的数据以特定方式排列,没有标题,并且列不一定包含相同类型的信息。其中一部分可以使用:

生成
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 个答案:

答案 0 :(得分:3)

使用GNU sed(因为地址后面的行数是扩展名):

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

编辑以添加awk解决方案:

awk '/NA/ { for ( i = 1; i <= 4; i++ ) { getline; } } { print }' infile

答案 1 :(得分: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)

答案 2 :(得分: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,]

答案 3 :(得分:0)

这可能适合你(GNU sed):

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

这将删除包含NA和任何后续0,...0

的所有行