Wednesday, 15 September 2010

how do you count unique factors and insert them into the same data frame in R -



how do you count unique factors and insert them into the same data frame in R -

dput(x) structure(list(state = structure(c(1l, 1l, 2l, 3l, 2l, 4l, 2l, 5l, 5l, 2l), .label = c("illinois", "texas", "california", "louisiana", "michigan"), class = "factor"), lat = structure(1:10, .label = c("41.627", "41.85", "32.9588", "33.767", "33.0856", "30.4298", "29.7633", "42.4687", "43.0841", "29.6919"), class = "factor"), long = structure(1:10, .label = c("-88.204", "-87.65", "-96.9812", "-118.1892", "-96.6115", "-90.8999", "-95.3633", "-83.5235", "-82.4905", "-95.6512"), class = "factor")), .names = c("state", "lat", "long"), row.names = c(na, 10l), class = "data.frame")

i need have column says total total count of each state. can creating column total:

x$total<-1

then

library(data.table x<-data.table(x) x<-x[,total:=sum(total),by=state]

is there better/shorter/efficient way of counting factors in info frame?

you can utilize dplyr (without having create total column):

(edit: @beginner enlightening me existence of n(), can more concise)

library('dplyr') mutate(group_by(x, state), total = n())

@beginner's solution of group_by(x, state) %>% mutate(total = n()) if need go on other manipulations of data. similarly,

x %>% group_by(state) %>% mutate(total = n())

will work, too.

r

No comments:

Post a Comment