Thanks for this, and all the other responses - I have a better understanding now.


David Winsemius wrote:

On May 30, 2010, at 1:43 AM, i...@whywouldwe.com wrote:

Hi

The docs for R are very practical, which is quite refreshing compared to other languages, however I can't find any details about all the things I can do with a vector. I'm expecting methods like vector.contains('foo') and vector.remove('foo'), maybe those methods don't exist but I'd love to find a page that details the ones that do.

Since vectors may contain named elements, your request is ambiguous. I cannot tell whether "foo" is a name or a value. It's also ambiguous because it's not clear what vector.contains() is supposed to return. If you read the introductory material, you should find that the function "[" is fairly fundamental to R operations.

?"["
?which
?grep

> x <- c(foo=3,bar=4)
> x
foo bar
  3   4
> x["foo"]
foo
  3
> x[-"foo"]
Error in -"foo" : invalid argument to unary operator
# the negation operation on indices only works with numeric arguments
# grep and which provide numeric resutls that can be used for "removal".
> x[grep("foo", names(x))]
foo
  3
> x[-grep("foo", names(x))]
bar
  4
> x[which(names(x)=="foo")]
foo
  3

Minus operations on numeric indices perform removal, at least to the extent of what is returned, but indexing does not do destructive removal unless accompanied by "<-" . In fact "[<-" is a function in its own right.


I'd also like to find some docs about foreach loops (unless there's a better way to iterate through a vector), I've only found mailing list posts.

Indexed solution are preferred, so see above, but if you want to find the help page for the "for" function

?"Control"



--

David Winsemius, MD
West Hartford, CT



______________________________________________
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