Putting a loop in a function

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Putting a loop in a function

Ben Hunter
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?
  for (i in 1:length(col)){
    num <- regexpr(str, col[i])
    if (num != -1){
         indices <- c(vector, i) # I've also had success with indices <-
append(indices, i)
  }
}
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.

Thanks,

-Ben

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Putting a loop in a function

Joshua Wiley-2
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 <[hidden email]> 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,

Josh


>
> -Ben
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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
http://www.joshuawiley.com/

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Putting a loop in a function

Berend Hasselman
In reply to this post by Ben Hunter
Ben Hunter 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?
  for (i in 1:length(col)){
    num <- regexpr(str, col[i])
    if (num != -1){
         indices <- c(vector, i) # I've also had success with indices <-
append(indices, i)
  }
}
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.
It is not clear what you want. Give a simple example with input and desired output.
About your function:

- what's the purpose of the <string> argument. It isn't being used in your function.
- what is the <vector> argument? Character? Object? in the function body vector is used as a built-in function.

Berend
Reply | Threaded
Open this post in threaded view
|

Re: Putting a loop in a function

Ben Hunter
In reply to this post by Joshua Wiley-2
Thanks so much Josh. That absolutely does help. I was just stuck on the
function regexpr and didn't consider how handy the other string handling
functions would be. As far as attaching 'i' to 'vector', that was indeed a
typo. As I was going through my code to make it more readable (at least that
was my intent), I forgot to switch the variable 'vector' to 'indices'. It
was the first of our weekly 'beer Fridays' at the office and I wasn't as
sharp an editor as I would have been otherwise. Nonetheless, I wanted to get
the e-mail off before I left.

I'll make sure to put some working code up next time.

-Ben



On Fri, Apr 1, 2011 at 7:08 PM, Joshua Wiley <[hidden email]> wrote:

> 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 <[hidden email]> 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,
>
> Josh
>
>
> >
> > -Ben
> >
> >        [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] 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
> http://www.joshuawiley.com/
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.