Calling labels from the table function in R -


i using r convert data experiment high quality dataset. 1 of features of code detect repetitions of experiment , label them accordingly. have written following code this:-

dayrep<-function(a){   caps<-c("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p",           "q","r","s","t","u","v","w","x","y","z")   if (unique(table(a))==1 && length(unique(table(a)))==1){     return(a)   }   else{     (i in a){       if (table(a)[[i]]>=2){         caps.sum<-caps[1:as.vector(table(a)[[i]])-1]         val<-c(i,paste0(i,caps.sum))         del<-a[!a %in% i]         vec<-append(del,val,after=i-1)         return(vec)       }     }   } } 

i have used following vectors of day numbers testing , highlight every possible outcome known far.

a<-c(1,2,3,4,5,6,7,8,9) b<-c(1,2,3,4,5,6,7,8,8) c<-c(1,2,3,3,4,5,6) d<-c(1,1,1,1,1,1) e<-c(1,2,2,3,4,5,6,6,7) f<-c(2,7,8,10,11,11,14) 

it produces following output:-

> dayrep(a) [1] 1 2 3 4 5 6 7 8 9 > dayrep(b) [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "8a" > dayrep(c) [1] "1"  "2"  "3"  "3a" "4"  "5"  "6"  > dayrep(d) [1] "1"  "1a" "1b" "1c" "1d" "1e" > dayrep(e) [1] "1"  "2"  "2a" "3"  "4"  "5"  "6"  "6"  "7"  > dayrep(f) error in table(a)[[i]] : subscript out of bounds 

the function works on tests e , f. e converts first set of repeated values, , f returns error message.

i aware problem being caused table(a)[[i]] element calling frequency value table, unsure whether or not there method call values being tabulated table. e.g.

> table(e) e 1 2 3 4 5 6 7  1 2 1 1 1 2 1 

the method using calling bottom line, wish call top line. know of solution this?

@cr1msonb1ade has kindly suggested use of make.unique function able perform above function slight variation.

> make.unique(e) [1] "1"   "2"   "2.1" "3"   "4"   "5"   "6"   "6.1" "7" 

thank you!


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -