Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

r - Subset data from a part of string

I have the following dataset:

dat2 <- read.table(header=TRUE, text="
ID  De  Ep  Ti  ID1
A1123    A117 121 100 11231
                   A1123MDN A108 C207 D110 E11232
                   A1124MDN A122 C207 D110 E11232
                   A1124MDN A117 C207 D110 E11232
                   A1124 A122 C208 D110 E11232
                   B1125MDN A108 C208 D110 E11232
                   B1125MDN A108 C208 D110 E11232
                   B1126MDN A122 C208 D110 E11233
                   C1126 A109 C208 D111 E11233
                   ")
dat2
        ID   De   Ep   Ti    ID1
1    A1123 A117  121  100  11231
2 A1123MDN A108 C207 D110 E11232
3 A1124MDN A122 C207 D110 E11232
4 A1124MDN A117 C207 D110 E11232
5    A1124 A122 C208 D110 E11232
6 B1125MDN A108 C208 D110 E11232
7 B1125MDN A108 C208 D110 E11232
8 B1126MDN A122 C208 D110 E11233
9    C1126 A109 C208 D111 E11233

I want to subset the dataset based on part of a sting MDN in first columns. The lengthy code is:

mdn <- subset(dat2, ID=="A1123MDN"|ID=="A1124MDN"| ID=="B1125MDN"| ID=="B1126MDN")
mdn
        ID   De   Ep   Ti    ID1
2 A1123MDN A108 C207 D110 E11232
3 A1124MDN A122 C207 D110 E11232
4 A1124MDN A117 C207 D110 E11232
6 B1125MDN A108 C208 D110 E11232
7 B1125MDN A108 C208 D110 E11232
8 B1126MDN A122 C208 D110 E11233

The data set has 456,345 rows. It is not feasible to subset by following the above way.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try with grep

dat2[grep("MDN",dat2$ID),]

or with grpl

subset(dat2, grepl("MDN",dat2$ID))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...