Reputation: 1770
I have a dataset with ID numbers and number of observations. I am writing a function to display output based on the IDs the user selects. The dataset have IDs starting from 1 to 332 and their corresponding observations.
Below is the code that I wrote:
complete=function(directory,id=1:332){
directory=read.csv(paste(path,"complete.csv",sep=""),header=TRUE,sep=",")
as.data.frame.matrix(directory)
mydf=na.omit(directory)
library(plyr)
completecount=count(mydf$ID)
colnames(completecount)=c("ID","nobs")
as.data.frame.matrix(completecount)
complete=subset(completecount,select=c(ID,nobs),subset=(completecount$ID%in%id))
return(complete)
}
When I am trying to run this
complete("specdata",4:2)
I would like to see the output in the order below based on the input above
ID nobs
2 4 474
3 3 243
4 2 1041
Whereas I am seeing output in the order as shown below
ID nobs
2 2 1041
3 3 243
4 4 474
Please advise what is wrong with my code.
Thanks for your help!
Upvotes: 0
Views: 124
Reputation: 206401
There's nothing in your function that ensures any order. I'm not sure if you think the %in%
operator also does sorting but it does not. It returns values no matter the order the second parameter is in. The order you are returning the rows is the order they were in in the original table.
Unlike %in%
, match()
actually does return values in order. You could do something like
library(plyr)
complete=function(directory,id=1:332){
directory=read.csv(paste(path,"complete.csv",sep=""),header=TRUE,sep=",")
as.data.frame.matrix(directory)
mydf=na.omit(directory)
completecount=count(mydf$ID)
colnames(completecount)=c("ID","nobs")
as.data.frame.matrix(completecount)
return(completecount[match(id, completecount$ID), ])
}
This will pluck rows out of completecount
in the order they they are passed to the function.
Upvotes: 2