# [R] help with algorithm

Joshua Wiley jwiley.psych at gmail.com
Sun Jul 31 23:04:07 CEST 2011

```Hi R Student,

On Sun, Jul 31, 2011 at 10:57 AM, r student <studentofr at gmail.com> wrote:
> I'm wondering if anyone can give some basic advice about how to approach a
>
> I'm new to R but have used SAS for many years, and while I can muscle
> through a lot of the code details, I'm unsure of a few things.
>
> Specific questions:
>
> If I have to perform a set of actions on a group of files, should I use a
> loop (I feel like I've heard people say to avoid looping in R)?

I think people suggesting avoiding loops in R when they are replacing
use of vectorized functions.  As an absurdly bad example of using a
loop:

> s <- 0
> for(i in 1:10) s <- s + (11:20)[i]
> s
[1] 155
> sum(11:20)
[1] 155

If you are doing the same computations on totally different datasets,
a loop may be pretty reasonable.  Also look at the *apply family of
functions.  Perhaps ?lapply though which is best depends on the task.

>
> How to get means for "by" groups and subset a files based on those (subset
> highest and lowest groups)?  (I can do this in multiple steps* but wonder
> what the best, "R way" is to do this.)

Here is one way to get the means by groups:

tmp <- with(mtcars, tapply(mpg, cyl, mean))
## and now subset by it
subset(mtcars, mtcars\$cyl %in% names(c(which.max(tmp), which.min(tmp))))

>
> How to draw cutoff lines at specific points on density plots?

plot(density(rnorm(100)))
abline(v = c(-1, 1))

See ?abline

>
> How to create a matrix of plots?  (Take 4 separate plots and put them into a
> single graphic.)

This depends a bit on the potential complexity of layouts you need.
See ?par and ?layout

dev.new() # start a new graphics device, just so the different options
do not overwrite each other
## and you can compare
par(mfrow = c(2, 2))
plot(lm(mpg ~ hp * wt, data = mtcars))

## layout is more flexible
dev.new()
layout(matrix(c(1, 2, 3, 1, 4, 3), ncol = 3, byrow = TRUE))
plot(lm(mpg ~ hp * wt, data = mtcars))

Basically the figure region is split into as many equal cells as the
matrix, and then cells that have the same number are merged into
bigger cells (or at least that is how I conceptualize what is going
on).  Then they are plotted in order (1, 2, 3, 4) wherever those are.

>
>
> * Get group means, add means back to file, sort by mean, take first and last
> groups

dat <- mtcars
dat\$gm <- with(mtcars, ave(mpg, cyl, FUN = mean))

and see before for subsetting.

Hope this helps,

Josh

>
> Feel free to excoriate me if I'm asking for too much help.  If possible
> though, a few words of advice (loops are the best way, just use the "main"
> parameter to combine plots) would be lovely if you can provide.
>
> Thanks!
>
>        [[alternative HTML version deleted]]
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.
>

--
Joshua Wiley
Ph.D. Student, Health Psychology
Programmer Analyst II, ATS Statistical Consulting Group
University of California, Los Angeles
https://joshuawiley.com/

```