[R] subset columns from list with variable substitution

MacQueen, Don macqueen1 at llnl.gov
Fri May 25 22:43:57 CEST 2012


The select argument to subset() is supposed to name the columns you want
to keep.
So is the syntax I gave, table[,list1], and it is the correct way when
list1 is a character vector (which it is).

Your error message says that at least one of the values in list1 is not
the name of a column in your data frame.


So, compare
  names(table)
with
   list1
(it looks like your data frame doesn't really have the column names you
think it has)

If you just type
  i %in% namelist
by itself, what do you get? Is it what you are trying to loop over?
Heck, what do you get from
  print(i)
?

If you are trying to loop over the names in namelist, you MUST use
  for (i in namelist)
the expression  i %in% namelist
will give you some combination of logical values TRUE and/or FALSE,
depending on the value of i at the time when you use the expression.

So if it "doesn't work", it's for some other reason. Also, just saying it
doesn't work isn't enough information. Please read and follow the posting
guide (mentioned at the end of every R-help email)

You are using i as a loop index, not only in

If I do your i %in% namelist in a fresh R session, I get:
  > i %in% namelist
  Error in match(x, table, nomatch = 0L) : object 'i' not found
So you must already have i defined. This is probably confusing the issue.




Also, since you are assigning a value to table2 inside the loop, the value
when the loop is done will be whatever it was the last time through the
loop.

By the way, table() is a built in R function, so 'table' not a good choice
for other uses.

Just for understanding R terminology, a list in R has a special structure.
You don't have any lists in your example. Your objects list1, list2,
namelist, group1, and group2 are all character vectors.

The help page for subset() gives examples of how to use the select
argument, and specifying a character vector is not one of the ways to use
it.


But given how you define list1, the correct "display" of it is

  > list1
  [1] "a" "b" "c" "d"

This appears to be what you want, so that's not the explanation.

Try
  length(list1)
  str(list1)
to learn a little more.



-- 
Don MacQueen

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





On 5/25/12 12:32 PM, "jween" <jween at rockwoodclinic.com> wrote:

>Thanks Don
>
>but 
>
>table[,list1]
>
>did not work either:
>
>Error in `[.data.frame`(table, , list1) : undefined columns selected.
>
>I'm guessing my list (list1) is not structured right? Displaying it has no
>commas, so the whole list may be taken as a single variable rather than a
>sequence of variables? I've tried various ways of reformatting (c(),
>as.list(), etc), but no go.
>
>also
>
>"i in namelist" does not work while
>"i %in% namelist" does. I don't really reference "i" in any function, only
>using it in the conditional.
>
>
>Any other suggestions?
>
>Thanks
>
>Jon
>
>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
>>
>>



More information about the R-help mailing list