Quantcast

subset columns from list with variable substitution

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

subset columns from list with variable substitution

jween
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: subset columns from list with variable substitution

MacQueen, Don
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" <[hidden email]> 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.
>
>______________________________________________
>[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: subset columns from list with variable substitution

jween
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: subset columns from list with variable substitution

MacQueen, Don
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" <[hidden email]> 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" <[hidden email]> 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
>>
>>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: subset columns from list with variable substitution

jween
Thanks Don

Please forgive my poor mail-liost etiquette.

I had a couple of errors:

1) the counter logic in the loop was "i in name list", my typo in the post.
2) There were typos in the variable list that caused the loop to crash when they were encountered.

Thanks for your help, really appreciated.

Best

Jon

On 2012-05-25, at 1:43 PM, MacQueen, Don wrote:

> 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" <[hidden email]> 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" <[hidden email]> 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
>>>
>>>
>
>
> ______________________________________________________________________
> This email has been scanned by the Symantec Email Security.cloud service.
> For more information please visit http://www.symanteccloud.com
> ______________________________________________________________________


______________________________________________________________________
This email has been scanned by the Symantec Email Security.cloud service.
For more information please visit http://www.symanteccloud.com

______________________________________________
[hidden email] 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.
Loading...