R: split data based on a factor, add a ranking column and extract -


i still haven't been able know how can access different elements of split data. here problem: have data set, want split based on column (state). want have ranking column added data each subset. part of function i'm writing. data set has 2 columns, hospital, state, outcome. each state, want add 'rank' column ranks data based on outcome; lowest outcome ranked 1 , highest outcome ranked last.

how can use split, sapply/lapply this? there better way, using "arrange"? main problem when use either of these methods, not know how access each element of split or arranged data. here's how data set looks like: hospital state outcome. row lines not important here.

                           hospital state outcome   1 southeast alabama medical center    al    14.3  2    marshall medical center south    al    18.5  3   eliza coffee memorial hospital    tx    18.1  7                st vincent's east    tx    17.7  8   dekalb regional medical center    al    18.0  9    shelby baptist medical center    al    15.9 

the desired outcome be

                           hospital state outcome rank  1 southeast alabama medical center    al    14.3    1  2 shelby baptist medical center       al    15.9    2  3 dekalb regional medical center      al    18.0    3                 4 marshall medical center south       al    18.5    4   5 st vincent's east                   tx    17.7    1   6 eliza coffee memorial hospital      tx    18.1    2  

thanks in advance.

the dplyr package provides elegant solution type of problem. i'm using mtcars data example:

library(dplyr)  mtcars %>%   group_by(cyl) %>%   mutate(rank = row_number(mpg)) 

Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -