1

I want to create a series of new objects for storing an analyses in, in a function. I want the function to have two arguments, one argument is a bunch of character strings and one is a numeric vector. Im fairly inexperienced with R so please if you could explain simply that would be much appreciated, with as much detail too, i see that a lot of people recommend the combination of paste and assign in situations similar to this, but i cant get it to work properly, and i also see a lot of recommendations for using lapply, but i cant get my head around how lapply is working, so if anyone has the skills to walk me through it in laymans terms it would be a massive help and much appreciated.

For example,

plant_species<-c("speciesA", "speciesB", "speciesC")
years<-(2005:2007)

which i would like to create the following objects from: speciesA2005, speciesA2006, speciesA2007, speciesB2006, speciesB2007, speciesB2008, speciesC2006, speciesC2007,speciesC2008.

But ideally I would like plant species and year to part of a function such as

myfunction<-function(year,species) {

}

which i could then give to someone else, so all they have to do is type

myfunction(2005:2007, "speciesA", "speciesB", "speciesC")

but it would be great if they could enter as many species as they like and the function recognised how many species they have entered and years and returned the appropriate number of objects

Many thanks

1
  • You definitely want 2008? (i.e. B & C sequences starting at one more than the start year of the input sequence and ending one more than the last year provided in the input sequence?) Commented Apr 20, 2014 at 2:34

3 Answers 3

2

I can modify this if my assumption of a typo for the desired output is true (i.e. if you really want 2005-2007 for all species and not 2005-2007 for the first one and then 2006-2008 for any remaining ones):

myfunction <- function(years, species) {
  dat <- expand.grid(species, years)[,1:2]
  return(sort(sprintf("%s%s", dat$Var1, dat$Var2)))
}

myfunction(years, plant_species)
## [1] "speciesA2005" "speciesA2006" "speciesA2007" "speciesB2005" "speciesB2006" "speciesB2007"
## [7] "speciesC2005" "speciesC2006" "speciesC2007"
Sign up to request clarification or add additional context in comments.

Comments

1

Here is a function that will behave exactly as you describe, as well as cases where the "species" is a vector in itself:

myfunction <- function(year, ...) {
  species <- list(...)
  if (length(species[[1]]) > 1) species <- rep(species[[1]], each = length(year))
  else species <- rep(unlist(list(...)), each = length(year))
  paste0(species, year)
}

myfunction(year=2005:2007, "A", "B", "C")
# [1] "A2005" "A2006" "A2007" "B2005" "B2006" "B2007" "C2005" "C2006" "C2007"

myfunction(2005:2007, c("A", "B", "C"))
# [1] "A2005" "A2006" "A2007" "B2005" "B2006" "B2007" "C2005" "C2006" "C2007"

Comments

0

There are many ways to solve this, and many packages have functions to do this. But, here is a very basic simple method:

# Step1: define an new empty vector
newVector <- NULL

# Step2:
# start a loop over one of the variables
# paste with all elements of second variables, and append newVector
for(spec in plant_species)   newVector <- c(b,paste(spec,years,sep=""))  

# see the results
newVector  

So the function will look like this:

myFunction <- function(years, plant_species) {
  newVector <- NULL
  for(spec in plant_species)   newVector <- c(b,paste(spec,years,sep=""))  
  return(newVector)
}

myFunction(years, plant_species)

Hope this helps!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.