RE: [R] manipulating dataframe according to the values of some columns

From: Francisco J. Zagmutt <>
Date: Sat 14 May 2005 - 05:41:07 EST

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


>From: "zhihua li" <>
>Subject: [R] manipulating dataframe according to the values of some columns
>Date: Fri, 13 May 2005 15:43:20 +0000
>hi netters,
>I'm a newbie to R and there are some very simple problems puzzeled me for
>two days.
>I've a dataframe here with several columns different in modes. Two of the
>columns are special for me: column 1 has the mode "factor" and column 2 has
>the mode "numeric vectors".
>The values for column 1 are either "T" or "F". I wanna do two things:
>Firstly, remove those rows whose values for column 1 are "F";
>Secondly,sort the rows in the ascending order of values for column 2.
>I believe the code to do these things is simple. But I can't figure it out.
>Please help me!
>Thanks a lot!
> mailing list
>PLEASE do read the posting guide!
> mailing list PLEASE do read the posting guide! Received on Sat May 14 05:51:50 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:45 EST