r - Check if a column contains a sequence -


i want know, if can check out if column of data frame starts 0 or 1 , goes till number of rows without breaking sequence. below sample data frame.

structure(list(x = 1:22, snr = c(1.0035798429, 11.9438978154,  na, 3.2894877794, 4.0170266411, 1.6310522977, 1.6405414787, 1.6625412522,  0.8489116253, 7.5312259672, 7.2832910726, 0.5732577083, na, 0.8149754292,  1.9981020389, 1.2477052103, 0.9960804911, 10.3402683931, 3.6328270728,  2.5540496855, 41.96873985, 6.2035281045), id = c(109l, 110l,  111l, 112l, 113l, 114l, 116l, 117l, 118l, 119l, 120l, 121l, 123l,  124l, 125l, 126l, 127l, 128l, 130l, 131l, 132l, 133l), signalintensity = c(6.8173738339,  11.5459925418, na, 9.7804203445, 9.8719842219, 9.0781857736,  8.2289312163, 8.0435364446, 6.1793458315, 10.5581798932, 10.4745329822,  4.1572943809, na, 6.0451742752, 8.3100219509, 7.4558770659, 7.1464749962,  11.4284386394, 9.6273795753, 9.6807417299, 13.3364944397, 10.4304671876 )), .names = c("x", "snr", "id", "signalintensity"), class = "data.frame", row.names = c(na,  -22l)) 

how can check columns , return index if present.

edited: sequence looking natural sequence. suppose if data frame has 10 rows, column if present should have sequence 1,2,3,4,5,6,7,8,9,10 or may like0,1,2,3,4,5,6,7,8,9. . sequence starts 0 or 1 , goes till number of rows increment of 1 each row.

you loop through columns sapply. create function check whether there nas. if not (!any), difference (diff) between adjacent element, check if element difference 1 (all(diff(x)==1) , (&) first value of column 0 or 1 (x[1] %in% 0:1). if there na, output column 'false'.

 f1 <- function(x) {           if(!any(is.na(x)))          all(diff(x)==1) & x[1] %in% 0:1          else false}  which(sapply(df, f1))  #x   #1  

Comments