user3745597
user3745597

Reputation: 159

Subsetting data in R with ifelse

I am attempting to use ifelse to subset data that can then be used in a plot. I am coding it this way as I am trying to make the code usable to a layman by only defining one or two objects and then running the whole script to make a plot using the data selected by given criteria.

The problem is that the mydataframe[mydataframe$data . ...] operation is not working the way I would like it to inside ifelse. Is there a way to get it to work in ifelse or is anyone aware of a smarter way to do what I'm trying to do? Thanks!

Also, the second block of code is added explanation but not needed to see the problem.

# generate data
mydata<-c(1:100)
mydata<-as.data.frame(mydata)
mydata$checkthefunction<-rep(c("One","Two","Three","Four","Multiple of 5",
                           "Six","Seven","Eight","Nine","Multiple of 10"))
# everything looks right
mydata

# create function
myfunction = function(MyCondition="low"){

  # special criteria
  lowRandomNumbers=c(58,61,64,69,73)
  highRandomNumbers=c(78,82,83,87,90)
  # subset the data based on MyCondition
  mydata<-ifelse(MyCondition=="low",mydata[mydata$mydata %in% lowRandomNumbers==TRUE,],mydata)
  mydata<-ifelse(MyCondition=="high",mydata[mydata$mydata %in% highRandomNumbers==TRUE,],mydata)
  # if not "high" or "low" then don't subset the data

  mydata
}

myfunction("low")
# returns just the numbers selected from the dataframe, not the 
# subsetted dataframe with the $checkthefunction row

myfunction("high")
# returns: "Error in mydata[mydata$mydata %in% highRandomNumbers == TRUE, ] : 
# incorrect number of dimensions"









# additional explanation code if it helps

# define dataframe again
mydata<-c(1:100)
mydata<-as.data.frame(mydata)
mydata$checkthefunction<-rep(c("One","Two","Three","Four","Multiple of 5",
                               "Six","Seven","Eight","Nine","Multiple of 10"))
# outside of the function and ifelse my subsetting works
lowRandomNumbers=c(58,61,64,69,73)
ItWorks<-mydata[mydata$mydata %in% lowRandomNumbers==TRUE,]

# ifelse seems to be the problem, the dataframe is cut into the string of lowRandomNumbers again
MyCondition="low"
NoLuck<-ifelse(MyCondition=="low",mydata[mydata$mydata %in% lowRandomNumbers==TRUE,],mydata)  
NoLuck

# if the 'else' portion is returned the dataframe is converted to a one-dimensional list
MyCondition="high"
NoLuck<-ifelse(MyCondition=="low",mydata[mydata$mydata %in% lowRandomNumber==TRUE,mydata)  
NoLuck               

Upvotes: 1

Views: 7257

Answers (1)

Roland
Roland

Reputation: 132576

You don't want ifelse. You want if and else. ifelse is used if you have a condition vector. You only have a single condition value.

myfunction = function(MyCondition="low"){

  # special criteria
  lowRandomNumbers=c(58,61,64,69,73)
  highRandomNumbers=c(78,82,83,87,90)
  # subset the data based on MyCondition
  mydata <- if(MyCondition=="low") mydata[mydata$mydata %in% lowRandomNumbers==TRUE,] else mydata
  mydata <- if(MyCondition=="high") mydata[mydata$mydata %in% highRandomNumbers==TRUE,] else mydata
  # if not "high" or "low" then don't subset the data

  mydata
}

Upvotes: 4

Related Questions