0

I have 2 different data frames and I am looking into trying to run the same function on each data table.

On every data frame there is a ClaimID variable and I would like to run a unique count on that variable for every table. The function i will be using will be the length combined with the unique function. Thanks!

this is the code i am using right now:

t <- data.frame(lapply(df.list,function(x) 
        cbind("PatientIDs", length(unique(x[,3])))))  

I believe i wast the outcome dataframe to look like this though:

FREQ    1    2    4    7
Christopher Yee
  • 535
  • 2
  • 5
  • 14
  • 2
    Please provide some example data and expected result – akrun May 12 '15 at 19:30
  • 2
    Try putting your data frames in a list and using `lapply` – Alex A. May 12 '15 at 19:30
  • 1
    If you show some example, it would be easier to test – akrun May 12 '15 at 19:33
  • Try `lapply(df.list, function(x) data.frame(id='patientids',table(x[,3])))` – akrun May 12 '15 at 19:36
  • Or may be `library(reshape2);table(melt(lapply(df.list,"[[", 3))[2:1])` – akrun May 12 '15 at 19:39
  • Can you test the solutions in the comment – akrun May 12 '15 at 19:46
  • i just tested them, they work but there is a new one that i cant get to work, its: u = data.frame(lapply(df.list,function(x)table(x[, 12]))) It returns the error: Error in data.frame(c(2L, 4L, 6L, 6L, 4L, 4L, 2L, 2L, 2L, 4L, 8L, 4L, : arguments imply differing number of rows: 157, 139, 125, 129 – Christopher Yee May 12 '15 at 19:54
  • 1
    Sorry, without a reproducible example, it is hard to know what is not working – akrun May 12 '15 at 19:55
  • 1
    @ChristopherYee: `lapply` is returning a list and you're trying to coerce it to a data frame. You're getting the error probably because the data frames in the list aren't the same size. But as akrun mentioned, it's pretty much impossible to know anything without a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Alex A. May 12 '15 at 19:57
  • sorry about that, thanks for the help, the data is just sensitive thats why i have to do it this way. – Christopher Yee May 12 '15 at 19:59
  • Can you check the solution posted below. It should work given the error you showed – akrun May 12 '15 at 20:01
  • @ChristopherYee: For future reference, you can always post made up sample data that's representative of the structure of your actual data. – Alex A. May 12 '15 at 20:26

1 Answers1

1

The reason why we got an error is because the lengths of table output are not equal for the list elements. To set the output lengths equal, we can convert the column to 'factor' with levels specified as the unique elements from all the list elements and then we use the table.

   Un1 <- sort(unique(unlist(lapply(df.list, `[[`, 12))))
   data.frame(lapply(df.list, function(x) table(factor(x[,12], levels=Un1))))

The below solution also give same frequency column output. In this case, you may not need to convert the column to 'factor'

  library(reshape2)
  table(melt(lapply(df.list,"[[", 12)))
akrun
  • 874,273
  • 37
  • 540
  • 662