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

Francisco J. Zagmutt gerifalte28 at hotmail.com
Fri May 13 21:41:07 CEST 2005


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


>From: "zhihua li" <lzhtom at hotmail.com>
>To: r-help at stat.math.ethz.ch
>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!
>
>______________________________________________
>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




More information about the R-help mailing list