[R] subset columns from list with variable substitution

MacQueen, Don macqueen1 at llnl.gov
Fri May 25 21:04:16 CEST 2012


Instead of
  subset(table, select=list1)
try
  table[, list1]

However, I suspect you have other problems.
Particularly, i is not defined when you use i %in% namelist.
You may have wanted
  i in namelist
  
-Don

-- 
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062





On 5/25/12 11:09 AM, "jween" <jween at rockwoodclinic.com> wrote:

>Hi there, I would like to use a list variable to select columns in a
>subset
>from a parent table:
>
>I have a data frame "table" with column headers a,b,c,d,e,x,y,z
>
>and list variables
>
>list1=c("a","b","c","d")
>list2=c("a","b","x",y","z")
>namelist=c("peter","paul","mary","jane")
>group1=c("peter","paul")
>group2=c("mary","jane")
>
>I would like to subset "table" based on the list variable in a for loop:
>
>for (i %in% namelist){
>     if (i %in% group1){table2<-subset(table, select=list1)}
>     else {{table2<-subset(table, select=list2)}
>}
>
>the "select=list1" syntax does not work. What would be the correct way to
>do
>this?
>
>Many Thanks
>
>Jon
>
>--
>View this message in context:
>http://r.789695.n4.nabble.com/subset-columns-from-list-with-variable-subst
>itution-tp4631374.html
>Sent from the R help mailing list archive at Nabble.com.
>
>______________________________________________
>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.



More information about the R-help mailing list