使用ifelse对R中的数据进行子集设置
我试图使用ifelse对数据进行子集划分,然后在绘图中使用这些数据。我是这样编码的,因为我试图通过只定义一个或两个对象,然后运行整个脚本,使用给定条件选择的数据生成一个绘图,从而使代码对外行可用 问题是mydataframe[mydataframe$data….]操作的工作方式与我希望它在ifelse中的工作方式不同。有没有办法让它在ifelse中工作?或者有没有人知道一种更聪明的方法来做我想做的事情?谢谢 另外,第二块代码添加了解释,但不需要查看问题使用ifelse对R中的数据进行子集设置,r,if-statement,subset,R,If Statement,Subset,我试图使用ifelse对数据进行子集划分,然后在绘图中使用这些数据。我是这样编码的,因为我试图通过只定义一个或两个对象,然后运行整个脚本,使用给定条件选择的数据生成一个绘图,从而使代码对外行可用 问题是mydataframe[mydataframe$data….]操作的工作方式与我希望它在ifelse中的工作方式不同。有没有办法让它在ifelse中工作?或者有没有人知道一种更聪明的方法来做我想做的事情?谢谢 另外,第二块代码添加了解释,但不需要查看问题 # generate data mydat
# 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
#生成数据
mydata您不需要ifelse
。您需要if
和else
<如果有条件向量,则使用code>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
}
myfunction=function(MyCondition=“低”){
#特殊标准
lowRandomNumbers=c(58,61,64,69,73)
高随机数=c(78,82,83,87,90)
#基于MyCondition对数据进行子集划分
mydata您不需要ifelse
。如果有条件向量,您需要if
和else
,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
}
myfunction=function(MyCondition=“低”){
#特殊标准
lowRandomNumbers=c(58,61,64,69,73)
高随机数=c(78,82,83,87,90)
#基于MyCondition对数据进行子集划分
我的数据好,这是一个比我预期的更容易的修复,它可以工作。谢谢!@Roland使用if和else,我必须在else中指定,如果不满足任何条件,则不更改数据帧?或者如果不满足“if”条件,R会自动不更改数据帧吗?这是一个比我预期的更容易的修复,它可以工作。谢谢!@Roland使用if和else,我是否必须在else中指定,如果不满足任何条件,则不更改数据帧?或者如果不满足“if”条件,R是否会自动不更改数据帧?