Skip to content

manipulating dataframe according to the values of some columns

2 messages · zhihua li, Francisco J. Zagmutt

#
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!
#
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