Steven T. Yen
2023-Jan-14 06:56 UTC
[R] Removing variables from data frame with a wile card
I have a data frame containing variables "yr3",...,"yr28". How do I remove them with a wild card----something similar to "del yr*" in Windows/doc? Thank you. > colnames(mydata) ? [1] "year"?????? "weight"???? "confeduc"?? "confothr" "college" ? [6] ... ?[41] "yr3"??????? "yr4"??????? "yr5"??????? "yr6" "yr7" ?[46] "yr8"??????? "yr9"??????? "yr10"?????? "yr11" "yr12" ?[51] "yr13"?????? "yr14"?????? "yr15"?????? "yr16" "yr17" ?[56] "yr18"?????? "yr19"?????? "yr20"?????? "yr21" "yr22" ?[61] "yr23"?????? "yr24"?????? "yr25"?????? "yr26" "yr27" ?[66] "yr28"...
mydata[, -grep("^yr",colnames(mydata))] On Sat, Jan 14, 2023 at 8:57 AM Steven T. Yen <styen at ntu.edu.tw> wrote:> I have a data frame containing variables "yr3",...,"yr28". > > How do I remove them with a wild card----something similar to "del yr*" > in Windows/doc? Thank you. > > > colnames(mydata) > [1] "year" "weight" "confeduc" "confothr" "college" > [6] ... > [41] "yr3" "yr4" "yr5" "yr6" "yr7" > [46] "yr8" "yr9" "yr10" "yr11" "yr12" > [51] "yr13" "yr14" "yr15" "yr16" "yr17" > [56] "yr18" "yr19" "yr20" "yr21" "yr22" > [61] "yr23" "yr24" "yr25" "yr26" "yr27" > [66] "yr28"... > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Andrew Simmons
2023-Jan-14 07:08 UTC
[R] Removing variables from data frame with a wile card
You'll want to use grep() or grepl(). By default, grep() uses extended regular expressions to find matches, but you can also use perl regular expressions and globbing (after converting to a regular expression). For example: grepl("^yr", colnames(mydata)) will tell you which 'colnames' start with "yr". If you'd rather you use globbing: grepl(glob2rx("yr*"), colnames(mydata)) Then you might write something like this to remove the columns starting with yr: mydata <- mydata[, !grepl("^yr", colnames(mydata)), drop = FALSE] On Sat, Jan 14, 2023 at 1:56 AM Steven T. Yen <styen at ntu.edu.tw> wrote:> > I have a data frame containing variables "yr3",...,"yr28". > > How do I remove them with a wild card----something similar to "del yr*" > in Windows/doc? Thank you. > > > colnames(mydata) > [1] "year" "weight" "confeduc" "confothr" "college" > [6] ... > [41] "yr3" "yr4" "yr5" "yr6" "yr7" > [46] "yr8" "yr9" "yr10" "yr11" "yr12" > [51] "yr13" "yr14" "yr15" "yr16" "yr17" > [56] "yr18" "yr19" "yr20" "yr21" "yr22" > [61] "yr23" "yr24" "yr25" "yr26" "yr27" > [66] "yr28"... > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Reasonably Related Threads
- Removing variables from data frame with a wile card
- Removing variables from data frame with a wile card
- Removing variables from data frame with a wile card
- Removing variables from data frame with a wile card
- Removing variables from data frame with a wile card