Re: [R] Removing variables from data frame with a wile card

2023-01-14 Thread avi.e.gross
John, I am very familiar with the evolving tidyverse and some messages a while back included people who wanted this forum to mainly stick to base R, so I leave out examples. Indeed, the tidyverse is designed to make it easy to select columns with all kinds of conditions including using r

Re: [R] Removing variables from data frame with a wile card

2023-01-14 Thread avi.e.gross
Valentin, You are correct that R does many things largely behind the scenes that make some operations fairly efficient. >From a programming point of view, though, many people might make a data.frame >and not think of it as a list of vectors of the same length that are kept that >way. So if th

Re: [R] Removing variables from data frame with a wile card

2023-01-14 Thread John Kane
You rang sir? library(tidyverse) xx = 1:10 yr1 = yr2 = yr3 = rnorm(10) dat1 <- data.frame(xx , yr1, yr2, y3) dat1 %>% select(!starts_with("yr")) or for something a bit more exotic as I have been trying to learn a bit about the "data.table package library(data.table) xx = 1:10 yr1 = yr2 = yr3

Re: [R] Removing variables from data frame with a wile card

2023-01-14 Thread avi.e.gross
Steven, Just want to add a few things to what people wrote. In base R, the methods mentioned will let you make a copy of your original DF that is missing the items you are selecting that match your pattern. That is fine. For some purposes, you want to keep the original data.frame and remove a

Re: [R] Removing variables from data frame with a wile card

2023-01-14 Thread Bill Dunlap
The -grep(pattern,colnames) as a subscript is a bit dangerous. If no colname matches the pattern then all columns will be omitted (because -0 is the same as 0, which means no column). !grepl(pattern,colnames) avoids this problem. > mydata <- data.frame(A=1:3,B=11:13) > mydata[, -grep("^yr", colna

Re: [R] Removing variables from data frame with a wile card

2023-01-14 Thread Steven Yen
Thanks to all. Very helpful. Steven from iPhone > On Jan 14, 2023, at 3:08 PM, Andrew Simmons wrote: > > 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