Wednesday, 18 September 2013

How do count all unique values in a data.frame

How do count all unique values in a data.frame

I've got a data frame with diagnoses as variables and patients as
observations. It's 32 variables and 5000 observations.
Please look at this example.
It is my goal to count and sum up all the diagnoses in the data frame
set.seed(1)
Data <- data.frame(id = seq(1, 10),
Diag1 = sample(c("A123", "B123", "C123"), 10, replace = TRUE),
Diag2 = sample(c("D123", "E123", "F123"), 10, replace = TRUE),
Diag3 = sample(c("G123", "H123", "I123"), 10, replace = TRUE),
Diag4 = sample(c("A123", "B123", "C123"), 10, replace = TRUE),
Diag5 = sample(c("J123", "K123", "L123"), 10, replace = TRUE),
Diag6 = sample(c("M123", "N123", "O123"), 10, replace = TRUE),
Diag7 = sample(c("P123", "Q123", "R123"), 10, replace = TRUE))
Data
class(Data)
mode(Data)
I know how to do it for one column using the plyr package
NoDiag1 <- count(Data, "Diag1")
How can I do this for the whole data frame instead of one variable?
If this is not possible, how can I add up column 1-7 to one column so that
I can use the count function for this "merged" column?

No comments:

Post a Comment