I have a set of file names like:
filelist <- c("filea-10.txt", "fileb-2.txt", "filec-1.txt", "filed-5.txt", "filef-4.txt")
and I would like to filter them according to the number after "-".
In python, for instance, I can use the key
parameter of the sorting function:
filelist <- ["filea-10.txt", "fileb-2.txt", "filec-1.txt", "filed-5.txt", "filef-4.txt"]
sorted(filelist, key=lambda(x): int(x.split("-")[1].split(".")[0]))
> ["filec-1.txt", "fileb-2.txt", "filef-4.txt", "filed-5.txt", "filea-10.txt"]
In R, I am playing with strsplit
and lapply
with no luck so far.
Which is the way to do it in R?
Edit:
File names can be many things and may include more numbers. The only fixed pattern is that the number I want to sort by is after the "-". Another (real) example:
c <- ("boards10017-51.mp4", "boards10065-66.mp4", "boards10071-81.mp4",
"boards10185-91.mp4", "boards10212-63.mp4", "boards1025-51.mp4",
"boards1026-71.mp4", "boards10309-89.mp4", "boards10310-68.mp4",
"boards10384-50.mp4", "boards10398-77.mp4", "boards10419-119.mp4",
"boards10421-85.mp4", "boards10444-87.mp4", "boards10451-60.mp4",
"boards10461-81.mp4", "boards10463-52.mp4", "boards10538-83.mp4",
"boards10575-62.mp4", "boards10577-249.mp4")"
See Question&Answers more detail:
os