Home > Blockchain >  I am getting error, while moving files in r
I am getting error, while moving files in r

Time:02-25

I am trying to move some files from one location to another using below code:

#exclude files with name .maf
filez <- grep(list.files(path="."), pattern='.maf', invert=TRUE, value=TRUE) 
filez <- data.frame(filez)
#select files with sepcial chr at 14 & 15 position in file name
Tumor <- filez %>% filter(between(as.integer(substr(filez, 14, 15)), 01, 09)) 
dir.create("Tumor")
file.move(Tumor$filez, "Tumor")

However I getting error

file.move(Tumor$filez, "Tumor")
Error in argchk_move_files(files = files, destinations = destinations,  : 
  Assertion on 'files' failed: Must be of type 'character', not 'factor'.

I donot know why error is coming.

CodePudding user response:

I've set up some files in a folder to illustrate:

> list.files(".")
[1] "abc01.maf" "abc02.maf" "abc03.maf" "abc04.maf" "abc05.maf" "abc06.maf"
[7] "abc07.maf"

Get some files into a vector (this is a bit different to you because I think you are getting all files that aren't .maf but this is close enough):

> filez <- grep(list.files(path="."), pattern='.maf', value=TRUE)
> filez
[1] "abc01.maf" "abc02.maf" "abc03.maf" "abc04.maf" "abc05.maf" "abc06.maf"
[7] "abc07.maf"

Now subset that vector using the condition derived from the numeric value in positions 4 and 5 being between 3 and 6:

> filez[dplyr::between(as.integer(substr(filez, 4,5)), 3, 6)]
[1] "abc03.maf" "abc04.maf" "abc05.maf" "abc06.maf"

Done. No need to create a data frame or use filter.

  • Related