[R] Assigning value of subset argument in "subset" function

David Winsemius dwinsemius at comcast.net
Mon Aug 24 06:36:42 CEST 2009


On Aug 24, 2009, at 12:24 AM, Steven Kang wrote:

> Hi David,
>
> I have another question regarding the R statement you have provided  
> which is:
>
>       for (zz in unique(a$xx) ) assign( paste("sub.a", zz, sep="."),  
> sum(subset(a, xx==zz)$xxx) )
>
> If the xx variables were characters instead of numerical values, is  
> there a way to execute the for loop as above?

I don't understand what the problem is (... no error message or  
reproducible code):

 > a <- data.frame(x=rep(1:4,2), y=rep(4:7,2), xx=rep(letters[8:11], 
2), xxx=rep(12:15,1))
 > a
   x y xx xxx
1 1 4  h  12
2 2 5  i  13
3 3 6  j  14
4 4 7  k  15
5 1 4  h  12
6 2 5  i  13
7 3 6  j  14
8 4 7  k  15

# the earlier code seems to still "work" without any modification

  for (zz in unique(a$xx) ) assign( paste("sub.a", zz, sep="."),  
sum(subset(a, xx==zz)$xxx) )
  sub.a.h; sub.a.i; sub.a.j; sub.a.k
[1] 24
[1] 26
[1] 28
[1] 30

Was that not what you expected?

-- 
David.

>
> Thanks.
> On Mon, Aug 24, 2009 at 1:33 PM, David Winsemius <dwinsemius at comcast.net 
> > wrote:
>
> On Aug 23, 2009, at 10:51 PM, Steven Kang wrote:
>
>> David, apreciate for your help.
>>
>> However, the result is not what I was expecting.
>>
>> Following on from your code, for example say the input is:
>>
>>    a <- data.frame(x=rep(1:4,2), y=rep(4:7,2), xx=rep(8:11,2),  
>> xxx=rep(12:15,1))
>>
>>           x y xx xxx
>>         1 1 4  8  12
>>         2 2 5  9  13
>>         3 3 6 10 14
>>         4 4 7 11 15
>>         5 1 4  8  12
>>         6 2 5  9  13
>>         7 3 6 10  14
>>         8 4 7 11  15
>>
>> Using the following "subset" function:
>>
>>    sub.a <- subset(a, xx == 8)
>>
>> results in:
>>
>>           x y xx xxx
>>        1 1 4  8  12
>>        5 1 4  8  12
>>
>> Now, I would like to assign the value of subsetted argument (i.e 8  
>> in this case)  to be able to use repetitively with other values.
>> For example,
>>
>>    sub.a.8 <- sum(sub.a$xxx)     to get sum of xxx (=24) satisfying  
>> the criteria where xx = 8
>>
>>    sub.a.9 <- sum(sub.a$xxx)     to get sum of xxx (=26) satisfying  
>> the criteria where xx == 9
>>    sub.a.10 <- sum(sub.a$xxx)   to get sum of xxx (=28) satisfying  
>> the criteria where xx == 10 etc
>
> Or perhaps even more programmatically:
>
> > for (zz in unique(a$xx) ) assign( paste("sub.a", zz, sep="."),  
> sum(subset(a, xx==zz)$xxx) )
>
>>
>> Please enlighten my problem.
>>
>> thanks
>>
>> On Mon, Aug 24, 2009 at 12:19 PM, David Winsemius <dwinsemius at comcast.net 
>> > wrote:
>>
>> On Aug 23, 2009, at 10:12 PM, David Winsemius wrote:
>>
>>
>> On Aug 23, 2009, at 9:56 PM, Steven Kang wrote:
>>
>> Dear R users,
>>
>> I am using "subset" function to filter out specific conditions and  
>> would
>> like to use the value of subsetted argument as a name of an object.
>>
>> Specifically, from the following statement:
>>
>>   a <- subset(dat, dat$x == "A" & dat$xx == 1 & dat$xxx == "AB" &  
>> dat$y
>> == "B" & dat$yy == 2)
>> I would to assign the value of the 3rd subset argument (i.e. "AB")  
>> as an
>> object's name such as:
>>
>>   a.AB <- sum(a$z)
>>
>> Sounds like you need to read FAQ 7.21:
>>
>> http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-can-I-turn-a-string-into-a-variable_003f
>>
>> Perhaps, ... lightly tested, of course, because no reproducible  
>> data provided:
>>
>> assign( paste("a.",  sum(a$z), sep=""),
>>       subset(dat,x == "A" & xx == 1 & xxx == "AB" &  y== "B" & yy  
>> == 2)   )
>>
>> # you do not need the "dat$"'s in the subset.
>>
>> > a <- data.frame(x=1:4, y=4:7, xx=8:11, xxx=12:15)
>> > a
>>  x y xx xxx
>> 1 1 4  8  12
>> 2 2 5  9  13
>> 3 3 6 10  14
>> 4 4 7 11  15
>>
>> Arrgh. Trimmed out the code:
>>
>> assign( paste("a.",  sum(a$x), sep=""),
>>         subset(a,x == 1 & xx == 8 )   )
>>
>>
>>
>> > a.10
>>  x y xx xxx
>> 1 1 4  8  12
>> -- 

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list