Loops, as in any program language, are sometimes unavoidable but this is
rarely the case in R. If you have any experience with MATLAB or similar
software, you know the importance of working vectorwise and the same
principles apply to R. For instance, if I wanted a list of all the squares
of numbers, I could write:

x = c(1,2,3,4,5,6,7,8,9,10)
for (i in 1:10) {
    temp = x[i]
    temp = temp^2
    print(temp)
}

but it will be much faster to write print(x^2), which handles the call to
"^2" once. More generally, you can work on selected subsets of vectors or
matrices using R's powerful "lexical scoping" nature. It's hard to give a
great example off the cuff (there are great introductions to R online that
can give better examples) but if you get started on a project, we can help
turn something written as a loop into a more R-like fashion. The "apply"
family of functions will also prove more than capable of replacing loops in
many circumstances when you get a little more comfortable with R.

I'm not sure what your second question means exactly, but if you have say a
matrix (or data frame) like this:

X Type
1 A
2 B
3 A
4 B
3 B
3 B
2 A
1 B
1 A
3 A
4 B

something like mean(subset(X,Type=="A")) might do what you're asking but I'd
be happy to help more if you can give a more concrete example.

The layout() command will do this for you.

If you ever get stuck on R there are quite a few good resources: first is to
simply google "introduction to R" and you'll get some wonderful tutorial
PDFs. Then, when you start getting your toes wet, you can use

> ? COMMAND

to get information about any command directly from the terminal.

Finally, looking through the archives of this list or more generally through
the r-seek.org engine will help you find more advanced answers.

Good luck getting started with R,

Michael Weylandt

PS -- One command I wish I knew when I got started is the apropos() command:
if you have a guess at the name of a function, but can't quite recall it, it
will help you find it.


On Sun, Jul 31, 2011 at 12:57 PM, r student <student...@gmail.com> wrote:

> I'm wondering if anyone can give some basic advice about how to approach a
> specific task in R.
>
> 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)?
>
> 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.)
>
> How to draw cutoff lines at specific points on density plots?
>
> How to create a matrix of plots?  (Take 4 separate plots and put them into
> a
> single graphic.)
>
>
> * Get group means, add means back to file, sort by mean, take first and
> last
> groups
>
>
>
> 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@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.
>

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