Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to using table in R?

Tags:

r

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
}
like image 839
Shawn Avatar asked Dec 01 '25 09:12

Shawn


1 Answers

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
like image 150
Dominic Comtois Avatar answered Dec 03 '25 22:12

Dominic Comtois



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!