r - Trim Data Based on First Character of Column Name -


i have data set multiple columns. using r want keep column have first character t create subset shown in output data below.

input data

t1234 t5678 t9101112 b d e   1     2       3    4 5 6 7   1     2       3    4 5 6 7   1     2       3    4 5 6 7   1     2       3    4 5 6 7   1     2       3    4 5 6 7   1     2       3    4 5 6 7   1     2       3    4 5 6 7 

output data

t1234 t5678 t9101112   1     2       3      1     2       3      1     2       3      1     2       3      1     2       3      1     2       3      1     2       3    

any suggestion how can achieved? thanks.

in base r using regex

df <- data.frame(t1234=rep(1,7),t5678=2,t9101112=3,a=4,b=5,d=6,e=7)  df[,grepl("^t",names(df))] 

the regex pattern ^t matches t @ beginning of each row name. refine pattern ^t\\d+ if wanted match "t" followed 1 or more numbers, example.

also note ^ asserts you're @ beginning of string. without you'd match "at912340" because contains t.

for multiple characters (i.e. columns start t or m) we'd use "or" operator |

df[,grepl("^t|m",names(df))] 

and match groups of characters rdy or mtp we'd this:

df[,grepl("^t|mtp|check|rdy",names(df))] 

note: in comments mistakenly used brackets so: [t,m]. using brackets tells regex match 1 of characters in brackets, in case match "t", "m", or ",". don't want match comma here, , it's syntactically incorrect have commas in brackets separating each character. match "t" or "m" correct syntax brackets [tm], however, match words, or short strings above, must use | as "or" operator.


Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -