使用ifelse在R中对数据进行子集

时间:2014-06-18 14:56:01

标签: r if-statement subset

我正在尝试使用ifelse来对可以在绘图中使用的数据进行子集化。我正在以这种方式编码,因为我试图通过仅定义一个或两个对象然后运行整个脚本来使用由给定标准选择的数据来创建绘图,从而使代码可用于外行。

问题是mydataframe [mydataframe $ data。 ......]操作不按照我想要的方式运行ifelse。有没有办法让它在ifelse中运行,或者是否有人知道更聪明的方法来做我想做的事情?谢谢!

此外,第二个代码块添加了解释,但不需要查看问题。

# 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               

1 个答案:

答案 0 :(得分:4)

你不想要ifelse。您想要ifelse。如果您有条件向量,则使用ifelse。您只有一个条件值。

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
}