简体   繁体   English

用ifelse在R中设置数据

[英]Subsetting data in R with ifelse

I am attempting to use ifelse to subset data that can then be used in a plot. 我正在尝试使用ifelse子集数据,然后可以在绘图中使用它。 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 . 问题是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? 有没有办法让它在ifelse中工作,或者有人知道做我想做的事的更聪明的方法吗? 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               

You don't want ifelse . 你不要ifelse You want if and else . 你想要ifelse ifelse is used if you have a condition vector. 如果您有条件向量,则使用ifelse 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM