I am using R in a Microsoft Windows environment. I have a dataset called “mp1b”. I have a variable called h. h can take a value from -1 to 5. If h <1, I want to create a new dataset called mp2 that is the same as mp1b: “mp2<-mp1b” If h > 0, I want to set create a dataset mp2, where I limit the original dataset to those where mp1b$group = =h. similar to: “mp2<-subset (mp1b, group= = h)” I have tried this ifelse statement, but it does not seem to work as expected. “mp2<-ifelse(h<1,mp1b,subset(mp1b,cluster_q==h))” Assistance is appreciated. --------------------------------- [[alternative HTML version deleted]]
Ferdinand Alimadhi
2005-Dec-13 19:23 UTC
[R] creating a subset of a dataset using ifelse statement?
?ifelse ; it's clear that you can use it in your case. Where cluster_q is comming from ( I suppose is the column name you want to filter by!)? What if you use the simpler syntax : if(h<1) mp2<-mp1 else mp2<-subset(mp1b,cluster_q==h) ? THT t c wrote:>I am using R in a Microsoft Windows environment. > > I have a dataset called "mp1b". I have a variable called h. > > h can take a value from -1 to 5. > > If h <1, I want to create a new dataset called mp2 that is the same as mp1b: > > "mp2<-mp1b" > > If h > 0, I want to set create a dataset mp2, where I limit the original dataset to those where mp1b$group = =h. similar to: > > "mp2<-subset (mp1b, group= = h)" > > I have tried this ifelse statement, but it does not seem to work as expected. > > "mp2<-ifelse(h<1,mp1b,subset(mp1b,cluster_q==h))" > > Assistance is appreciated. > > >--------------------------------- > > > [[alternative HTML version deleted]] > > > >------------------------------------------------------------------------ > >______________________________________________ >R-help@stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >-- Ferdinand Alimadhi Programmer / Analyst Harvard University The Institute for Quantitative Social Science (617) 496-0187 falimadhi@latte.harvard.edu www.iq.harvard.edu [[alternative HTML version deleted]]
Uwe Ligges
2005-Dec-13 20:57 UTC
[R] creating a subset of a dataset using ifelse statement?
t c wrote:> I am using R in a Microsoft Windows environment. > > I have a dataset called ?mp1b?. I have a variable called h. > > h can take a value from -1 to 5. > > If h <1, I want to create a new dataset called mp2 that is the same as mp1b: > > ?mp2<-mp1b? > > If h > 0, I want to set create a dataset mp2, where I limit the original dataset to those where mp1b$group = =h. similar to: > > ?mp2<-subset (mp1b, group= = h)? > > I have tried this ifelse statement, but it does not seem to work as expected. > > ?mp2<-ifelse(h<1,mp1b,subset(mp1b,cluster_q==h))?mp2 <- if(h<1) mp1b else subset(mp1b,cluster_q==h) Uwe Ligges> Assistance is appreciated. > > > --------------------------------- > > > [[alternative HTML version deleted]] > > > > ------------------------------------------------------------------------ > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html