Saturday 15 January 2011

r - counting number of observations into a dataframe -



r - counting number of observations into a dataframe -

i want create new column in dataframe states number of observations particular group. have surgical procedure (hrg.code) , multiple consultants perform procedure (consultant.code) , length of remain patients in in days.

using

sourcedata2$meanvalue<-with(sourcedata2,ave(lengthofstaydays., hrg.code, consultant.code fun=mean))

i can new column (meanvalue) shows mean length of remain per consultant per procedure. need. however, i'd know how many occurances of each procedures each consultant performed new column in same info frame.

how generate number of observations. there doesn't appear fun = observations or fun = freq capability.

you may try:

tbl <- table(sourcedata2[,3:2]) #gives frequency of each `procedure` i.e. `hrg.code` done every `consultant.code` tbl # hrg.code #consultant.code b c # 1 1 0 # b 4 2 1 # c 0 0 1 # d 1 1 1 # e 2 0 0 as.data.frame.matrix(tbl) #converts `table` `data.frame`

if want total unique procedures done each consultant.code in long form.

with(sourcedata2, as.numeric(ave(hrg.code, consultant.code, fun=function(x) length(unique(x))))) # [1] 3 3 3 2 1 3 3 3 3 1 1 3 3 3 2 data sourcedata2 <- structure(list(lengthofstaydays = c(2l, 2l, 4l, 3l, 4l, 5l, 2l, 4l, 5l, 2l, 4l, 2l, 4l, 4l, 2l), hrg.code = c("c", "a", "a", "b", "a", "a", "b", "c", "a", "a", "c", "a", "b", "b", "a"), consultant.code = c("b", "b", "b", "a", "e", "b", "d", "d", "d", "e", "c", "b", "b", "b", "a")), .names = c("lengthofstaydays", "hrg.code", "consultant.code"), row.names = c(na, -15l), class = "data.frame")

r data.frame frequency

No comments:

Post a Comment