0

So I currently have this code below in a file, pullsec.R

pullsec <- function(session=NULL){
  if(is.null(session))  session<-1
  stopifnot(is.numeric(session))
  paste("Session",1:10)[session]
}

In an .Rnw file, I call on this pullsec.R and choose session number 3 by:

source("pullsec.R")
setsec <- pullsec(3)

which would pull all of the rows where the column Session has data values of "Session 3"

I would like to add another block to pullsec.R that would allow me to pull data for a second column, Sessions where the data in that column is Sessions 1-2, Sessions 3-4, Session 5-6, etc. But I'm not sure how to modify the pullsec block to accept multiple inputs.

I had tried many solutions but no bite. My most naive solution is:

pullsec2 <- function(sessions1=NULL,sessions2=NULL){
  if(is.null(sessions1))  sessions1<-1
  stopifnot(is.numeric(session1))
  paste("Sessions",1:10,"-",1:10)[sessions]
}
user12279
  • 21
  • 1
  • 4

1 Answers1

0

either of these would do:

pullsec2 <- function(session1=1,session2=2){
  stopifnot(is.numeric(session1))
  stopifnot(is.numeric(session2))
  paste0("Sessions ",session1,'-',session2)
}
pullsec2(3,4)

pullsec2 <- function(sessions=1){
  stopifnot(is.numeric(sessions))
  paste("Sessions",paste0(sessions,collapse="-"))
}

pullsec2(3:4)
Jthorpe
  • 9,756
  • 2
  • 49
  • 64