0

I am getting this error after I use a dplyr function. The loop I have uses matrix (mymat), and then in another step, since dplyr is extremely fast, I want to convert that matrix to dataframe (mydf) and run dplyr function. So this is what I did:

 loop begins
    #several other function which spit out mymat

        mydf<-as.data.frame(mymat) 
        library(dplyr)
        library(stringi)
          #this is my dplyr function        
          mydata<-data.frame(mydf, check.names = FALSE) %>%
          mutate_each(funs(stri_replace_all(., REF, fixed = "0")), ends_with(".GT")) %>%
          mutate_each(funs(stri_replace_all(., ALT, fixed = "1")), ends_with(".GT")) %>%
          mutate_each(funs(stri_replace_all(., " ", fixed = "/")), ends_with(".GT")) %>%
          mutate_each(funs(stri_replace_all(., "0 0", fixed = "NA")), ends_with(".GT")) %>%
          select(ends_with(".GT")) %>%
          t()

    loop ends

I don't now why I get this error. Please also note that I need to work with matrix in preceding steps and therefore I can't start with the dataframe.

Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘select’ for signature ‘"data.frame"’

The sample data is here:

 mymat<-structure(c("G", "A", "C", "A", "G", "A", "C", "T", "G", "A", 
"1/1", "0/0", "0/0", "NA", "NA", "0,15", "8,0", "8,0", "NA", 
"NA", "1/1", "0/1", "0/0", "NA", "NA", "0,35", "12,12", "15,0", 
"NA", "NA"), .Dim = 5:6, .Dimnames = list(c("chrX:133511988:133511988:G:A:snp", 
"chrX:133528116:133528116:A:C:snp", "chrX:133528186:133528186:C:T:snp", 
"chrX:133560301:133560301:A:G:snp", "chrX:133561242:133561242:G:A:snp"
), c("REF", "ALT", "02688.GT", "02688.AD", "02689.GT", "02689.AD"
)))
MAPK
  • 5,635
  • 4
  • 37
  • 88
  • Could you provide some example data, that would make things much easier. http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Mhairi McNeill Oct 01 '15 at 10:33
  • 1
    Why do you load both `plyr` and `dplyr`? For the part of the loop that you showed, you only need `dplyr`. – Jaap Oct 01 '15 at 10:34
  • @Jaap Sorry I corrected that. – MAPK Oct 01 '15 at 10:40
  • 2
    Maybe first make sure that the command `select` belongs only to `dplyr` package and that this is the one you're using. An easy way is to use `dplyr::select` instead and see if it works. – AntoniosK Oct 01 '15 at 10:40
  • @MhairiMcNeill Thanks, I have updated the question with sample data. – MAPK Oct 01 '15 at 10:41
  • 1
    You also need to change to `mydf<-as.data.frame(mymat) `. A dot is missing there. – AntoniosK Oct 01 '15 at 10:43
  • The code runs fine for me, except as `as.dataframe` should be `as.data.frame`. Does the code you've posted above work for you? – Mhairi McNeill Oct 01 '15 at 10:44
  • @MhairiMcNeill It does for this sample data. However, when I convert the matrix into dataframe within the loop, I get the error. I have another function that takes matrix but the execution time is almost 300 times slow. – MAPK Oct 01 '15 at 10:47
  • @AntoniosK Sorry that was a typo – MAPK Oct 01 '15 at 10:48
  • 1
    @MAPK what about my previous comment? Did you try `dplyr::select` instead of just `select`? – AntoniosK Oct 01 '15 at 11:00
  • 1
    If you could narrow down exactly the data and exactly the function that's causing the problem it would be easier to help. I've found this information about debugging useful: http://www.stats.uwo.ca/faculty/murdoch/software/debuggingR/ – Mhairi McNeill Oct 01 '15 at 11:02
  • @AntoniosK Just tried that and worked fine. Thanks guys! – MAPK Oct 01 '15 at 11:02
  • Great! Hope the `dplyr` guys are aware of that. I've found similar issues here: https://github.com/hadley/dplyr/issues/643 and https://github.com/hadley/dplyr/issues/756 . Do you know which package caused that problem? Do you really need that package for your process? If yes, then use the `dplyr::select`. – AntoniosK Oct 01 '15 at 11:32
  • @AntoniosK Yes, dplyr or any other faster data manipulation package is more relevant in my case. Thanks for the help! – MAPK Oct 01 '15 at 11:45

0 Answers0