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

Please enlighten my problem.

thanks




On Mon, Aug 24, 2009 at 12:19 PM, David Winsemius <dwinsem...@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
>
>

        [[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.

Reply via email to