Thank you! Works like a charm!
On Thu, Mar 27, 2014 at 12:19 PM, David Carlson <dcarl...@tamu.edu> wrote: > That only requires two small changes in Sarah's first solution: > > > Finaldata[, !colnames(Finaldata) %in% V$v2[V$v1 == 1]] > Length Rate > 1 0.53607323 0.01739951 > 2 0.15405615 0.11837908 > 3 0.04542388 0.53702702 > 4 0.15633703 0.68870041 > 5 0.35293973 0.38258981 > > ------------------------------------- > David L Carlson > Department of Anthropology > Texas A&M University > College Station, TX 77840-4352 > > > -----Original Message----- > From: r-help-boun...@r-project.org > [mailto:r-help-boun...@r-project.org] On Behalf Of Sneha Bishnoi > Sent: Thursday, March 27, 2014 11:06 AM > To: Sarah Goslee > Cc: r-help > Subject: Re: [R] Subsetting a dataframe by dynamic column name > > Hi Sarah, > > Thanks! Do agree its over complicated. > However looking at the solutions I think I did not state my > problem > completely. > V provides choices for only certain set of columns in Finaldata. > So v2 may not represent all columns of Finaldata. > I want to retain columns not provided as a choice for users plus > the ones > user chooses to keep. > > Thanks! > > > > On Thu, Mar 27, 2014 at 11:30 AM, Sarah Goslee > <sarah.gos...@gmail.com>wrote: > > > There are many ways. You're making it overly complicated > > > > Here, in an actual reproducible example (as you were requested > to submit): > > > > V <- data.frame(v1=c(1,0,0), v2=c("Shape", "Length", "Rate"), > > stringsAsFactors=FALSE) > > Finaldata <- data.frame(Shape = runif(5), Length = runif(5), > Rate = > > runif(5)) > > > > # assuming names in V are not in the same order as columns in > Finaldata > > # also assuming there might accidentally be names not in > Finaldata > > Finaldata[, colnames(Finaldata) %in% V$v2[V$v1 == 0]] > > > > # more elegant? > > Finaldata[, colnames(Finaldata) %in% V$v2[!V$v1]] > > > > # not robust to errors in V > > Finaldata[, V$v2[!V$v1]] > > > > # assumes order of column names matches order of V > > Finaldata[, -V$v1] > > > > Sarah > > > > On Thu, Mar 27, 2014 at 11:09 AM, Sneha Bishnoi > <sneha.bish...@gmail.com> > > wrote: > > > Hi all! > > > > > > I am trying to drop columns from a data frame dynamically > depending on > > user > > > input. The dataframe whose columns need to be dropped is > called > > "Finaldata" > > > So here is what I do: > > > > > > V is a dataframe with columns v1 and v2 as follows > > > > > > v1 v2 > > > 1 1 Shape > > > 2 0 Length > > > 3 0 Rate > > > > > > v1 corresponds to user input, 1 if you want to drop the > column, 0 > > otherwise > > > v2 corresponds to column names of the all the columns in > "Finaldata" > > > I then use following code to drop columns > > > > > > for (i in 1:3) > > > { > > > if(V$v1[i]==1) > > > { > > > print(V$v2[i]) > > > Finaldata<-subset(Finaldata,select=-c(V$v2[i])) > > > } > > > > > > } > > > > > > However v2 being type character is not accepted by subset. > > > I read subset needs column names without quotes. > > > I tried stripping off quotes through gsub and cat,however it > din't help > > > There are lot of columns and I cannot perform this > individually on all > > > columns. > > > How do i go about it? > > > > > > > > > Thanks! > > > SB > > > > > > > -- > > Sarah Goslee > > http://www.functionaldiversity.org > > > > > > -- > Sneha Bishnoi > +14047235469 > H. Milton Stewart School of Industrial & Systems Engineering > Georgia Tech > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. > > -- Sneha Bishnoi +14047235469 H. Milton Stewart School of Industrial & Systems Engineering Georgia Tech [[alternative HTML version deleted]] ______________________________________________ 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.