R - Sorting and Sub-setting Maximum Values within Columns -
i trying iteratively sort data within columns extract n maximum values.
my data set first , second columns containing occupation titles , codes, , of rest of columns containing comparative values (in case location quotients had calculated each city) occupations various cities:
*occ_code city1 ... city300* occ1 5 ... 7 occ2 20 ... 22 . . . . . . . . occ800 20 ... 25
for each city want sort maximum values, select subset of maximum values matched respective occupations titles , titles. thought relatively trivial but...
edit clarification: want end sorted subset of data analysis.
occ_code city1 occ200 10 occ90 8 occ20 2 occ95 1.5
at same time want able repeat sort column-wise (so i've tried lots of order commands through calling columns directly: data[,2]; able run same analysis functions on entire dataset.
i've been messing plyr past 3 days , feel setup of dataset not conducive how plyer meant used.
i'm not sure desired output according example snippit. here's how data frame every city using plyr
, reshape
#using same df nico's answer library(reshape) df.m <- melt(df, id = 1) a.cities <- cast(df.m, codes ~ . | variable) library(plyr) a.cities.max <- aaply(a.cities, 1, function(x) arrange(x, desc(`(all)`))[1:4,])
now, a.cities.max
array of data frames, 4 largest values each city in each data frame. 1 of these data frames, can index with
a.cities.max$x13
i don't know you'll doing data, might want in data frame format.
df.cities.max <- adply(a.cities.max, 1)
Comments
Post a Comment