r - Can ls() have a pattern > 1? -
say have 10 data.frame
s contain dates in names 01-01-00
10-01-00
other letters , symbols added end in format ddmmyy
e.g. 010100/sgh/d_3
and 020100/aff/d_1
if wanted create vector of above data.frame
, there way select them without writing them out individually?
i tried creating vector of sequence of dates , putting in pattern =
came error (code below):
dates <- seq(as.date("2000-01-01"),as.date("2000-01-02"),1) dates <- format(dates,"%d%m%y") ls(pattern=dates) in grep(pattern, all.names, value = true) : argument 'pattern' has length > 1 , first element used
i'm assuming pattern can 1 value?
create pattern matches of date strings want. 1 way join strings |
characters:
> dates <- seq(as.date("2000-01-01"),as.date("2000-01-10"),1) > dates [1] "2000-01-01" "2000-01-02" "2000-01-03" "2000-01-04" "2000-01-05" [6] "2000-01-06" "2000-01-07" "2000-01-08" "2000-01-09" "2000-01-10" > d2 = paste(dates,collapse="|") > d2 [1] "2000-01-01|2000-01-02|2000-01-03|2000-01-04|2000-01-05|2000-01-06|2000-01-07|2000-01-08|2000-01-09|2000-01-10"
now have workspace various bits , pieces in:
> ls() [1] "d" "d010100foo" "d010110bar" "d2" [5] "d2000-01-01bar" "d2000-01-10bar" "d2000-02-10foo" "dates" [9] "dorig" "j" "p" "x" [13] "y" "z"
but if use pattern ones match dates:
> ls(pattern=d2) [1] "d2000-01-01bar" "d2000-01-10bar"
this unwieldy if had lots of things match, that point can write simpler regexp match things - [0-9]{4}-[0-9]{2}-[0-9]{2}
(untested) should match 4 digit, dash, 2 digit, dash, 2 digit sequence.
adjust pattern match date format. format have used not recommended: https://xkcd.com/1179/
Comments
Post a Comment