r - sort data.frame by column alphabetically in dplyr without converting to factor -


i'm working data using dplyr package , need sort data.frame 1 of columns alphabetically. problem column character , dplyr arranges data.frame in wrong manner. here's example.

require("dplyr") set.seed(7) df1 <- data.frame(name = sample(c("a", "a", "b", "b"), 20, t),                    value = rnorm(20), stringsasfactors = f) str(df1) # 'data.frame':  20 obs. of  2 variables: #      $ name : chr  "b" "a" "a" "a" ... #      $ value: num  0.357 2.717 2.281 0.324 1.896 ... df1 %>% group_by(name) %>% summarise(val = sum(value)) # source: local data frame [4 x 2] #  #   name       val # 1    5.4297509 # 2    b 0.8402506 # 3    3.8079681 # 4    b 0.7522799 

here in column name first go capitals , small letters. arrange doesn't change anything. if make name variable factor becomes arranged in right order. that's

set.seed(7) df2 <- data.frame(name = sample(c("a", "a", "b", "b"), 20, t),                    value = rnorm(20), stringsasfactors = t) df2 %>% group_by(name) %>% summarise(val = sum(value)) # source: local data frame [4 x 2] #  #   name       val # 1    3.8079681 # 2    5.4297509 # 3    b 0.7522799 # 4    b 0.8402506     

in data processing i'd keep name of character type, , breaking %>%-pipes of dplyr code code isn't nice too, so, possible dplyr sort first df1 df2 ?


Comments