3

I have a function called notes_count(id) that takes a vector as a parameter (for example the function can accept different arguments 5, c(1,2,3), 6:20, or 5:1 to name a few) and returns the ID and "count" of the notes. I have a data frame with the following contents:

"ID" "Date" "Notes"

that contains an unknown amount of entries per "ID" for example:

ID  Date Notes 
1   xxx  "This is a note"
1   xxx  "More notes here"
...
8   xxx  "Hello World"

The problem I am running into is that I want the output to be ordered in the same way as the input vector meaning notes_count(3:1) should list the results in reverse order as a data frame:

  ID notes_count
1  3    6
2  2    288
3  1    102

and calling notes_count(1:3) would result in:

  ID notes_count
1  1    102
2  2    288
3  3    6

however table always reorders from min to max despite the order it is given originally. Is there a way to do what table is doing directly on the data frame but using other functions so that I can control the output.

Currently my code is this:

#Before calling table I have data frame "notes" in the order I want but table reorders it
notes_count <- as.data.frame(table(notes[["ID"]]))

which seems silly to make the original data frame a table and then convert it back.

EDIT:

Here is my code as basic as it is as requested

notes_count <- function(id){
## notes.csv format
## "ID","Date","Notes"
## 1,"2016-01-01","Some notes"

#read the csv to a data frame
notes <- read.csv("notes.csv")

#remove all NA values
notes <- notes[complete.cases(notes), ]

#here is where you can order the data but it won't matter when aggregating the notes to a "count" using table on the next line
notes <- notes[id, ]

#convert the table back to a data frame
notes_count <- as.data.frame(table(notes[["ID"]]))

notes_count
}
Shawn
  • 3,583
  • 8
  • 46
  • 63
  • It would help if you provided a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data and desired output. I don't understand where you are using the function parameter here. – MrFlick Feb 29 '16 at 03:03
  • `x <- c(2,1,3,3); table(x)[unique(x)]` - is that sort of what you want? – thelatemail Feb 29 '16 at 03:04
  • @MrFlick Code has been added. It is as basic as it gets – Shawn Feb 29 '16 at 03:21
  • @thelatemail I added my code so you can see what I am attempting to do. I have banged my head on this issue for hours. – Shawn Feb 29 '16 at 03:35

2 Answers2

3

Here's a simplified example that should get you going:

set.seed(1234)
notes <- data.frame(id=sample(2:10,size = 100, replace = TRUE), Note="Some note")

notes_count <- function(id) {
  counts <- table(notes[notes$id %in% id,])
  return(data.frame(count=counts[as.character(id),]))
}

notes_count(c(10,2,5))

# Results

   count
10     8
2     12
5      2
Dominic Comtois
  • 10,230
  • 1
  • 39
  • 61
  • Dominic this got me very close to the answer I needed. I modified it a bit and will post the answer. The piece that helped was creating an explicit data frame with the output from the table itself. So simple yet subtle. Thanks! – Shawn Feb 29 '16 at 05:49
  • Glad I could help! :) – Dominic Comtois Feb 29 '16 at 06:04
0

If I understand correctly, you want to sort the dataframe by the notes_count variable?

then use order function and reshuffle the df rows.

your_data_frame[order(your_data_frame$notes_count,decreasing=TRUE),]
Jan Sila
  • 1,554
  • 3
  • 17
  • 36
  • The problem is I want it to be based on the vector input. In other words I don't know if the input will be 1:5 or 5:1. I don't always want it to be decreasing, it depends on the input. – Shawn Feb 29 '16 at 02:58