I have a folder with multiple csv and ords files.
And I have a vector with specific name of files which I need to used.
I want to list files from this folder by vector files
,
Below is my code I have tried,but not got what I desired.
files <- c("log3","log4","log6","log8","log9","log10","log11","log29","log30","log31","log34","log35","log37")
pattern <- paste(files, sep="", collapse="|")
l1 <- list[grepl(pattern, list)]
Then I got result but not match with vector
l1
>
[1] "D:/data/log10.csv"
[2] "D:/data/log11.csv"
[3] "D:/data/log29.csv"
[4] "D:/data/log3.csv"
[5] "D:/data/log30.csv"
[6] "D:/data/log31.csv"
[7] "D:/data/log32.csv" # not in vector
[8] "D:/data/log33.csv" # not in vector
[9] "D:/data/log34.csv"
[10] "D:/data/log35.csv"
[11] "D:/data/log36.csv" # not in vector
[12] "D:/data/log37.csv"
[13] "D:/data/log38.csv" # not in vector
[14] "D:/data/log4.csv"
[15] "D:/data/log40.csv" # not in vector
[16] "D:/data/log41.csv" # not in vector
[17] "D:/data/log42.csv" # not in vector
[18] "D:/data/log43.csv" # not in vector
[19] "D:/data/log44.csv" # not in vector
[20] "D:/data/log45.csv" # not in vector
[21] "D:/data/log46.csv" # not in vector
[22] "D:/data/log47.csv" # not in vector
[23] "D:/data/log48.csv" # not in vector
[24] "D:/data/log6.csv"
[25] "D:/data/log60.csv" # not in vector
[26] "D:/data/log61.csv" # not in vector
[27] "D:/data/log62.csv" # not in vector
[28] "D:/data/log63.csv" # not in vector
[29] "D:/data/log64.csv" # not in vector
[30] "D:/data/log65.csv" # not in vector
[31] "D:/data/log66.csv" # not in vector
[32] "D:/data/log67.csv" # not in vector
[33] "D:/data/log68.csv" # not in vector
[34] "D:/data/log69.csv" # not in vector
[35] "D:/data/log8.csv"
[36] "D:/data/log80.csv" # not in vector
[37] "D:/data/log81.csv" # not in vector
[38] "D:/data/log82.csv" # not in vector
[39] "D:/data/log83.csv" # not in vector
[40] "D:/data/log84.csv" # not in vector
[41] "D:/data/log85.csv" # not in vector
[42] "D:/data/log86.csv" # not in vector
[43] "D:/data/log87.csv" # not in vector
[44] "D:/data/log88.csv" # not in vector
[45] "D:/data/log9.csv"
[46] "D:/data/log93.csv" # not in vector
l1 <- c(
"D:/data/log10.csv",
"D:/data/log11.csv",
"D:/data/log29.csv",
"D:/data/log3.csv",
"D:/data/log30.csv",
"D:/data/log31.csv",
"D:/data/log32.csv",
"D:/data/log33.csv",
"D:/data/log34.csv",
"D:/data/log35.csv",
"D:/data/log36.csv",
"D:/data/log37.csv",
"D:/data/log38.csv",
"D:/data/log4.csv",
"D:/data/log40.csv",
"D:/data/log41.csv",
"D:/data/log42.csv",
"D:/data/log43.csv",
"D:/data/log44.csv",
"D:/data/log45.csv",
"D:/data/log46.csv",
"D:/data/log47.csv",
"D:/data/log48.csv",
"D:/data/log6.csv",
"D:/data/log60.csv",
"D:/data/log61.csv",
"D:/data/log62.csv",
"D:/data/log63.csv",
"D:/data/log64.csv",
"D:/data/log65.csv",
"D:/data/log66.csv",
"D:/data/log67.csv",
"D:/data/log68.csv",
"D:/data/log69.csv",
"D:/data/log8.csv",
"D:/data/log80.csv",
"D:/data/log81.csv",
"D:/data/log82.csv",
"D:/data/log83.csv",
"D:/data/log84.csv",
"D:/data/log85.csv",
"D:/data/log86.csv",
"D:/data/log87.csv",
"D:/data/log88.csv",
"D:/data/log9.csv",
"D:/data/log93.csv"
)
How do I only get specific files from files.Thank you.
The problem you have is that you're not adding the period to each one, so "log6" matches "D:/data/log6.csv", but also "D:/data/log60.csv", "D:/data/log61.csv" etc.
pattern <- paste(files, collapse="\\.|")
grep(pattern, l1, value=TRUE)