Skip to content
Prev 69697 / 398526 Next

manipulating dataframe according to the values of some columns

Hi Zhihua

Try the following:

dat=data.frame(x=rep(c("T","F"),10),y=(runif(20)))#Creates data frame like 
in your example
newdat=dat[dat$x=="T",] #includes only rows with variable x equal to "T"
newdat=newdat[order(newdat[,"y"], decreasing=FALSE),]# sorts in ascending 
order the newdat #data by the values of y.  Notice that the default is 
order(decreasing=FALSE) but I added that argument so you can see that you 
can also sort descending.

Another alternative to the second line of code is to use the higher level 
function subset() i.e.:
newdat=subset(dat, x=="T",select=c(x,y))#again, the select argument is 
optional in this example but I added it so you can see how you can select 
specific coumns for your subset.

I hope that this helps

Francisco