Hello, I have two very basic questions (console attached): 1) What am I getting an error message for # 5 and # 7 ? 2) How to fix the code? I would appreciate receiving your help. Thanks, Pradip Muhuri ###### Reproducible Example ##### N <- 100 set.seed(13) df<-data.frame(matrix(sample(c(1:10),N, replace=TRUE),ncol=5)) keep_var <- c("X1", "X2") drop_var <- c("X3", "X4", "X5") df[df$X1>=8,] [,1:2] #1 df[df$X1>=8,] [,-c(3,4,5)] #2 df[df$X1>=8,] [,c(-3,-4,-5)] #3 df[df$X1>=8,] [,c("X1", "X2")] #4 df[df$X1>=8,] [,-c("X3", "X4", "X5")] #5 DOES NOT WORK df[df$X1>=8,] [,keep_var] #6 df[df$X1>=8,] [, !drop_var] #7 DOES NOT WORK
Hi Pradip, It is easier to use subset(). Check ?subset for some examples and pay special attention to the "select" parameter. By the way, do not call your data "df" as it is already a function. Best, Jorge.- On Sat, Nov 24, 2012 at 1:55 PM, Muhuri, Pradip (SAMHSA/CBHSQ) <> wrote:> > Hello, > > I have two very basic questions (console attached): > > 1) What am I getting an error message for # 5 and # 7 ? > 2) How to fix the code? > > I would appreciate receiving your help. > > Thanks, > > Pradip Muhuri > > > > ###### Reproducible Example ##### > > N <- 100 > set.seed(13) > df<-data.frame(matrix(sample(c(1:10),N, replace=TRUE),ncol=5)) > > keep_var <- c("X1", "X2") > drop_var <- c("X3", "X4", "X5") > > > df[df$X1>=8,] [,1:2] #1 > df[df$X1>=8,] [,-c(3,4,5)] #2 > df[df$X1>=8,] [,c(-3,-4,-5)] #3 > df[df$X1>=8,] [,c("X1", "X2")] #4 > df[df$X1>=8,] [,-c("X3", "X4", "X5")] #5 DOES NOT WORK > df[df$X1>=8,] [,keep_var] #6 > df[df$X1>=8,] [, !drop_var] #7 DOES NOT WORK > > ______________________________________________ > R-help@r-project.org mailing list > 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]]
HI, This should work: df[df$X1>=8,][-which(names(df)%in% c("X3","X4","X5"))] #?? X1 X2 #1?? 8? 2 #5? 10? 1 #8?? 8? 5 #9?? 9? 4 #12? 9? 5 #13? 9 10 #19? 9? 8 ?df[df$X1>=8,][,!names(df)%in%drop_var] #?? X1 X2 #1?? 8? 2 #5? 10? 1 #8?? 8? 5 #9?? 9? 4 #12? 9? 5 #13? 9 10 #19? 9? 8 A.K. ----- Original Message ----- From: "Muhuri, Pradip (SAMHSA/CBHSQ)" <Pradip.Muhuri at samhsa.hhs.gov> To: "r-help at r-project.org" <r-help at r-project.org> Cc: Sent: Friday, November 23, 2012 9:55 PM Subject: [R] subsetting - questions Hello, I have two very basic questions (console attached): 1) What am I getting an error message for? # 5 and # 7 ? 2) How to fix the code? I would appreciate receiving your help. Thanks, Pradip Muhuri ###### Reproducible Example? ##### N <- 100 set.seed(13) df<-data.frame(matrix(sample(c(1:10),N, replace=TRUE),ncol=5)) keep_var <- c("X1", "X2") drop_var <- c("X3", "X4", "X5") df[df$X1>=8,] [,1:2]? ? ? ? ? ? ? ? ? #1 df[df$X1>=8,] [,-c(3,4,5)]? ? ? ? ? ? #2 df[df$X1>=8,] [,c(-3,-4,-5)]? ? ? ? ? #3 df[df$X1>=8,] [,c("X1", "X2")]? ? ? ? #4 df[df$X1>=8,] [,-c("X3", "X4", "X5")]? #5? DOES NOT WORK df[df$X1>=8,] [,keep_var]? ? ? ? ? ? ? #6 df[df$X1>=8,] [, !drop_var]? ? ? ? ? ? #7? DOES NOT WORK ______________________________________________ R-help at r-project.org mailing list 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.
Hi Arun, Thank you so much for your help. Pradip ________________________________________ From: arun [smartpink111 at yahoo.com] Sent: Friday, November 23, 2012 10:15 PM To: Muhuri, Pradip (SAMHSA/CBHSQ) Cc: R help Subject: Re: [R] subsetting - questions HI, This should work: df[df$X1>=8,][-which(names(df)%in% c("X3","X4","X5"))] # X1 X2 #1 8 2 #5 10 1 #8 8 5 #9 9 4 #12 9 5 #13 9 10 #19 9 8 df[df$X1>=8,][,!names(df)%in%drop_var] # X1 X2 #1 8 2 #5 10 1 #8 8 5 #9 9 4 #12 9 5 #13 9 10 #19 9 8 A.K. ----- Original Message ----- From: "Muhuri, Pradip (SAMHSA/CBHSQ)" <Pradip.Muhuri at samhsa.hhs.gov> To: "r-help at r-project.org" <r-help at r-project.org> Cc: Sent: Friday, November 23, 2012 9:55 PM Subject: [R] subsetting - questions Hello, I have two very basic questions (console attached): 1) What am I getting an error message for # 5 and # 7 ? 2) How to fix the code? I would appreciate receiving your help. Thanks, Pradip Muhuri ###### Reproducible Example ##### N <- 100 set.seed(13) df<-data.frame(matrix(sample(c(1:10),N, replace=TRUE),ncol=5)) keep_var <- c("X1", "X2") drop_var <- c("X3", "X4", "X5") df[df$X1>=8,] [,1:2]? #1 df[df$X1>=8,] [,-c(3,4,5)]? #2 df[df$X1>=8,] [,c(-3,-4,-5)]? #3 df[df$X1>=8,] [,c("X1", "X2")]? #4 df[df$X1>=8,] [,-c("X3", "X4", "X5")]? #5 DOES NOT WORK df[df$X1>=8,] [,keep_var]? #6 df[df$X1>=8,] [, !drop_var]? #7 DOES NOT WORK ______________________________________________ R-help at r-project.org mailing list 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.
On 2012-11-23 18:55, Muhuri, Pradip (SAMHSA/CBHSQ) wrote:> > Hello, > > I have two very basic questions (console attached): > > 1) What am I getting an error message for # 5 and # 7 ? > 2) How to fix the code? > > I would appreciate receiving your help. > > Thanks, > > Pradip Muhuri > > > > ###### Reproducible Example ##### > > N <- 100 > set.seed(13) > df<-data.frame(matrix(sample(c(1:10),N, replace=TRUE),ncol=5)) > > keep_var <- c("X1", "X2") > drop_var <- c("X3", "X4", "X5") > > > df[df$X1>=8,] [,1:2] #1 > df[df$X1>=8,] [,-c(3,4,5)] #2 > df[df$X1>=8,] [,c(-3,-4,-5)] #3 > df[df$X1>=8,] [,c("X1", "X2")] #4 > df[df$X1>=8,] [,-c("X3", "X4", "X5")] #5 DOES NOT WORK > df[df$X1>=8,] [,keep_var] #6 > df[df$X1>=8,] [, !drop_var] #7 DOES NOT WORKTo see what's wrong, just print the problematic part: -c("X3", "X4", "X5") You can't negate a character vector; you have to have a numeric vector. And !drop_var doesn't work because you need something that evaluates to a logical value if you want to "!" it. This will do it: df[df$X1>=8,] [, !names(df) %in% drop_var] Or use the subset() function, as Jorge suggests. Peter Ehlers
Hi Jorge, I could use subset(). But, I wanted to minimize coding. Thanks, Pradip ________________________________________ From: Jorge I Velez [jorgeivanvelez at gmail.com] Sent: Friday, November 23, 2012 10:02 PM To: Muhuri, Pradip (SAMHSA/CBHSQ) Cc: r-help at r-project.org Subject: Re: [R] subsetting - questions Hi Pradip, It is easier to use subset(). Check ?subset for some examples and pay special attention to the "select" parameter. By the way, do not call your data "df" as it is already a function. Best, Jorge.- On Sat, Nov 24, 2012 at 1:55 PM, Muhuri, Pradip (SAMHSA/CBHSQ) <> wrote: Hello, I have two very basic questions (console attached): 1) What am I getting an error message for # 5 and # 7 ? 2) How to fix the code? I would appreciate receiving your help. Thanks, Pradip Muhuri ###### Reproducible Example ##### N <- 100 set.seed(13) df<-data.frame(matrix(sample(c(1:10),N, replace=TRUE),ncol=5)) keep_var <- c("X1", "X2") drop_var <- c("X3", "X4", "X5") df[df$X1>=8,] [,1:2] #1 df[df$X1>=8,] [,-c(3,4,5)] #2 df[df$X1>=8,] [,c(-3,-4,-5)] #3 df[df$X1>=8,] [,c("X1", "X2")] #4 df[df$X1>=8,] [,-c("X3", "X4", "X5")] #5 DOES NOT WORK df[df$X1>=8,] [,keep_var] #6 df[df$X1>=8,] [, !drop_var] #7 DOES NOT WORK ______________________________________________ R-help at r-project.org<mailto:R-help at r-project.org> mailing list 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.