[R] Putting a loop in a function

Joshua Wiley jwiley.psych at gmail.com
Sat Apr 2 04:08:05 CEST 2011

Hi Ben,

I am having some trouble figuring out what it is exactly you want.  A
workable example would be nice and then (even if just manually) typed
out what you would like it to return based on a given input.  I almost
think you just want grep().

grep("test", c("test", "not", "test2", "not"), value = TRUE)
[1] "test"  "test2"

using your example:

grep("string", d[,"Column.You.Want"], value = TRUE)

On Fri, Apr 1, 2011 at 5:39 PM, Ben Hunter <bjameshunter at gmail.com> wrote:
> I'm stuck here. The following code works great if I just use it on the
> command line in a 'for' loop. Once I try to define it as a function as seen
> below, I get bad results. I'm trying to return a vector of column indices
> based on a short string that is contained in a selection (length of about
> 70) of long column names that changes from time to time. Is there an
> existing function that will do this for me? The more I think about this
> problem, the more I feel there has to be a function out there. I've not
> found it.
> ind <- function(col, string, vector){  # this is really the problem. I don't
> feel like I'm declaring these arguments properly.
>  indices <- vector(mode = 'numeric') # I am not entirely confident that
> this use is necessary. Is indices <- c() okay?

indices <- c() would sort of work, but vector() is better.  Also, if
you actually want your function to be returning integers, you should
instatiate indices as an integer class vector, not numeric.

>  for (i in 1:length(col)){
>    num <- regexpr(str, col[i])

str() is a function, and as far as I can tell, a variable "str" is not
defined anywhere in your function or your functions argument.  Did you
mean "string"?

>    if (num != -1){
>         indices <- c(vector, i) # I've also had success with indices <-
> append(indices, i)

why are you combining "vector" with i over and over?  This will give
you something like:

c("vector", i1, "vector", i2, "vector", i3, etc.) except obviously
replace i1, i3 with their values and "vector" with its value.  Is that
what you want?

>  }
> }
> indices
> }
> ind(d[,'Column.I.want'], 'string', 'output.vector')
> Am I wrong here? I've read that the last statement in the function is what
> it will return, and what I want is a vector of integers.

yes, if return() is not explicitly specified inside the function, then
it will return the output of the last statement.

> Thanks,

Thank you for providing code of what you tried.  For future reference,
it is good to at least provide useable input data and then show us
what the output you would like is.  For example: "Blah blah blah, I
have a vector d <- c(1, 2, 3), how can I find the average of this
(i.e., 2)?" To which you would get the answer: "mean(d)" or some such.

If grep() is not actually what you are after, can you let us know a
little bit more about what you want?

Hope this helps,


> -Ben
>        [[alternative HTML version deleted]]
> ______________________________________________
> 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.

Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles

More information about the R-help mailing list