Skip to content

Putting a loop in a function

4 messages · Ben Hunter, Joshua Wiley, Berend Hasselman

#
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:
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.
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"?
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?
yes, if return() is not explicitly specified inside the function, then
it will return the output of the last statement.
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 Hunter wrote:
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  argument. It isn't being used in your function.
- what is the  argument? Character? Object? in the function body vector is
used as a built-in function.

Berend


--
View this message in context: http://r.789695.n4.nabble.com/Putting-a-loop-in-a-function-tp3421439p3421768.html
Sent from the R help mailing list archive at Nabble.com.