Skip to content
Prev 267538 / 398502 Next

Writing a summary file in R

Just a very simple follow-up. In the summary table (listed as "summ" below),
the "TR" column I would like to display the total number of rows (i.e.
counts) which I have done via "NROW()" function.

However, in the "RG1" I would only like to count the number of rows with a
'totalread' count >= 1 (i.e. rows that don't contain zero).

This may be confusing given the data I've provided, but values in the
'totalreads' column don't have to be 1 or 0, they can be any value.
Therefore using "sum()" won't work in every case.

As you can see I've tried using NROW() below for "RG1" but it didn't work
out like I had planned.

For example, given the input data, "chr4 100 300" should have RG1=1 and
percent=0.5. Instead, it just counts every row regardless of value.

The solution is probably something very simple I'm overlooking, but if you
could help I'd appreciate it.

Below is the code I've slightly modified from David's reply:
###############################Code##############################
#this is the input file
####################################
     chr start end base1 base2 totalreads methylation strand
1   chr1   100 159   104   104          1        0.05      +
2   chr1   100 159   145   145          1        0.04      +
3   chr1   200 260   205   205          1        0.12      +
4   chr1   500 750   600   600          1        0.09      +
5   chr3   450 700   500   500          1        0.03      +
6   chr4   100 300   150   150          1        0.05      +
7   chr4   100 300   175   175          0        0.00      +
8   chr7   350 600   400   400          1        0.06      +
9   chr7   350 600   550   550          0        0.00      +
10  chr9   100 125   100   100          1        0.10      +
11 chr11   679 687   680   680          1        0.07      +
12 chr11   679 687   681   681          0        0.00      +
13 chr22   100 200   105   105          1        0.03      +
14 chr22   100 200   110   110          1        0.08      +
15 chr22   300 400   350   350          0        0.00      +
####################################
#######################
          end TR RG1 percent
chr1 100  159  2   2       1
chr1 200  260  1   1       1
chr1 500  750  1   1       1
chr11 679 687  2   2       1
chr22 100 200  2   2       1
chr22 300 400  1   1       1
chr3 450  700  1   1       1
chr4 100  300  2   2       1
chr7 350  600  2   2       1
chr9 100  125  1   1       1
#######################
#the finished output
###########################################
          end TR RG1 percent Min. 1st Qu. Median  Mean 3rd Qu. Max.
chr1 100  159  2   2       1 0.04  0.0425  0.045 0.045  0.0475 0.05
chr1 200  260  1   1       1 0.12  0.1200  0.120 0.120  0.1200 0.12
chr1 500  750  1   1       1 0.09  0.0900  0.090 0.090  0.0900 0.09
chr11 679 687  2   2       1 0.00  0.0175  0.035 0.035  0.0525 0.07
chr22 100 200  2   2       1 0.03  0.0425  0.055 0.055  0.0675 0.08
chr22 300 400  1   1       1 0.00  0.0000  0.000 0.000  0.0000 0.00
chr3 450  700  1   1       1 0.03  0.0300  0.030 0.030  0.0300 0.03
chr4 100  300  2   2       1 0.00  0.0125  0.025 0.025  0.0375 0.05
chr7 350  600  2   2       1 0.00  0.0150  0.030 0.030  0.0450 0.06
chr9 100  125  1   1       1 0.10  0.1000  0.100 0.100  0.1000 0.10
############################################

##############################################################
David Winsemius wrote:
--
View this message in context: http://r.789695.n4.nabble.com/Writing-a-summary-file-in-R-tp3700031p3716837.html
Sent from the R help mailing list archive at Nabble.com.