# Re: [R] Function for subset of cases/lines

From: Stefan Uhmann <stefan.uhmann_at_mailbox.tu-dresden.de>
Date: Thu, 15 May 2008 10:53:29 +0200

Dear Dimitris, dear Matthias,

thank you very much, I adapted the short solution you both provided - works as intended. And I have learned sth. once again.

Regards,
Stefan

Dimitris Rizopoulos schrieb:
> try this:
>
> q1 <- c(4660,5621,5629,8030,8080,8180,8501,8190,8370,8200)
>
> "number height
> 1 4660 2.5
> 2 5010 1.4
> 3 5621 0.8
> 4 5629 2.3
> 5 8030 2.5
> 6 8080 2.4
> 7 8090 0.9
> 8 8180 1.4
> 9 8501 1.2
> 10 8190 1.9
> 11 8200 2.0
> 12 8370 2.1
> 13 8200 1.8"), header = TRUE)
> closeAllConnections()
>
> mean(with(dat, height[number %in% q1]))
> # or
> mean(dat\$height[dat\$number %in% q1])
>
>
> I hope it helps.
>
> Best,
> Dimitris
>
> ----
> Dimitris Rizopoulos
> Biostatistical Centre
> School of Public Health
> Catholic University of Leuven
>
> Address: Kapucijnenvoer 35, Leuven, Belgium
> Tel: +32/(0)16/336899
> Fax: +32/(0)16/337015
> Web: http://med.kuleuven.be/biostat/
> http://www.student.kuleuven.be/~m0390867/dimitris.htm
>
>
> ----- Original Message ----- From: "Stefan Uhmann"
> <stefan.uhmann_at_mailbox.tu-dresden.de>
> To: <r-help_at_stat.math.ethz.ch>
> Sent: Thursday, May 15, 2008 10:07 AM
> Subject: [R] Function for subset of cases/lines
>
>

```>> Hi,
>>
>> I have a vector:
>>
>> q1<-c(4660,5621,5629,8030,8080,8180,8501,8190,8370,8200)
>>
>> The following command gives me the mean of its elements:
>>
>> mean(q1)
>> [1] 7346.1
>>
>> What can I do to do the same for the variable 'height', but only for
>> the cases/rows which have one of the elements of q1 as 'number':
>>
>>   number height
>> 1 4660 2.5
>> 2 5010 1.4
>> 3 5621 0.8
>> 4 5629 2.3
>> 5 8030 2.5
>> 6 8080 2.4
>> 7 8090 0.9
>> 8 8180 1.4
>> 9 8501 1.2
>> 10 8190 1.9
>> 11 8200 2.0
>> 12 8370 2.1
>> 13 8200 1.8
>>
>> Thanks for any help,
>> Stefan
>>
>> ______________________________________________
>> R-help_at_r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
```

>
>
> Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
>
>

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. Received on Thu 15 May 2008 - 12:21:07 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 15 May 2008 - 12:30:43 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.