1

I need some help, I have a table in R with so may columns, lets say that I am interested in the columns V111 to V135 and I would like to count the rows in each column that are different to './.'.

I can do this for each column independently (it says to me the number of coincidences different to ./.)

m <-p[p$V111 != './.', ]

but I appreciate I someone could suggest me a loop to get the number of coincidences for each column.

V110    V111    V112
./. 1/1:0,51:51:99:2136,153,0   1/1:0,28:28:84:1211,84,0
./. ./. 0/1:15,13:28:99:434,0,543
./. ./. ./.
./. ./. ./.
./. ./. ./.
1/1:0,21:21:63:875,63,0 ./. ./.
./. ./. ./.
./. ./. ./.
./. ./. ./.
./. ./. ./.
./. ./. ./.
./. 1/1:0,18:18:54:745,54,0 1/1:0,5:5:15:207,15,0
./. 1/1:0,2:2:6:90,6,0  ./.
./. 1/1:0,2:2:6:90,6,0  ./.
./. ./. ./.
./. ./. ./.
./. ./. ./.
./. 0/1:6,4:10:99:137,0,210 ./.
Mia Lua
  • 157
  • 1
  • 11
  • 1
    Please read [How to make a great reproducible example in R?](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to make your question more likely to get an answer. – M-- Aug 18 '17 at 15:27
  • 1
    @d.b *`V111` **to** `V135`* not "and". – M-- Aug 18 '17 at 15:28
  • 1
    `table(p != "./.", col(p))`, probably. It would help to see an example. – Frank Aug 18 '17 at 15:35

3 Answers3

3

You could count the number of rows without './.' in each column with colSums(). For the data you provided, if you wanted to count rows without './.' in the first two columns, you'd do

colSums(p[,1:2] != "./.")
#V110 V111 
#   1    5 

If you wanted the rows in each column where there is no './.', you'd do

lapply(p[,1:2], function(x) which(x != "./."))
#$V110
#[1] 6

#$V111
#[1]  1 12 13 14 18
d.b
  • 32,245
  • 6
  • 36
  • 77
1

If your data frame is p you can check the number of rows different than './.' in every specified column by

 colnames_p <-names(p[, 111:135]) #corresponds to V111 to V135
 for ( i in seq_along(colnames_p)){
   print(names(p[i]))
   m <-length(p[p[i] != './.', ])
   print(m)
}
Patrik_P
  • 3,066
  • 3
  • 22
  • 39
1

Following code will provide you with the desired output:

colSums(p[,111:135 != "./.")

where, 111:135 is the number of columns.

Sagar
  • 2,778
  • 1
  • 8
  • 16