Sort data frame column by factor

后端 未结 4 2666
自闭症患者
自闭症患者 2020-12-06 00:43

Supose I have a data frame with 3 columns (name, y, sex) where name is character, y is a numeric value and <

4条回答
  •  没有蜡笔的小新
    2020-12-06 01:19

    It sounds to me like you're trying to order by score within the males and females and return a combined data frame of sorted males and sorted females.

    You are right that by(score, score$sex, function(x) x[order(x$y),]) returns a list of sorted data frames, one for male and one for female. You can use do.call with the rbind function to combine these data frames into a single final data frame:

    do.call(rbind, by(score, score$sex, function(x) x[order(x$y),]))
    #           x         y sex
    # F.5    EMMA  7.526866   F
    # F.9  VIOLET  8.182407   F
    # F.3   SUSAN  9.677511   F
    # M.4   LARRY  6.929395   M
    # M.8    MATT  7.970015   M
    # M.7     TIM  8.297137   M
    # M.6 LEONARD  8.845588   M
    # M.2     TOM  9.035948   M
    # M.1    MARK 10.082314   M
    

提交回复
热议问题