mydf <- read.table(text="date - site1 - site2 - site3 - site4
1/1/00 - 24 - 33 - 10 - 13
2/1/00 - 13 - 25 - 6 - 2", sep="-", header=TRUE)
# find ranks
t(apply(-mydf[-1], 1, rank))## site1 site2 site3 site4
## [1,] 2 1 4 3
## [2,] 2 1 3 4
# add to your dates
mydf.rank <- cbind(mydf[1], t(apply(-mydf[-1], 1, rank)))fname<-c("Joe", "Bob", "Bill", "Tom", "Sue","Sam","Jane","Ruby")
score<-c(500, 490, 500, 750, 550, 500, 210, 320)
dat<-data.frame(fname,score)
order.scores<-order(dat$score,dat$fname)
dat$rank <- NA
dat$rank[order.scores] <- 1:nrow(dat)
dat## fname score rank
## 1 Joe 500 5
## 2 Bob 490 3
## 3 Bill 500 4
## 4 Tom 750 8
## 5 Sue 550 7
## 6 Sam 500 6
## 7 Jane 210 1
## 8 Ruby 320 2
dat <- data.frame(fname=c("Joe", "Bob", "Bill", "Tom", "Sue","Sam","Jane","Ruby"),
score=c(500, 490, 500, 750, 550, 500, 210, 320))
order.scores <- order(dat$score)
dat1 <- dat[order.scores,]
dat1$rank <- rank(dat1$score)
dat1## fname score rank
## 7 Jane 210 1
## 8 Ruby 320 2
## 2 Bob 490 3
## 1 Joe 500 5
## 3 Bill 500 5
## 6 Sam 500 5
## 5 Sue 550 7
## 4 Tom 750 8
df <- data.frame(id=c(1,2,3,4,5,6),
profile_id = c(1, 1, 1, 2, 2,2),
company = c("A","A","A","B","B","B"),
product = c("book", "shirt", "cup","book", "shirt", "cup"),
price = c(10.42, 23.91, 5.95, 7.99, 5.95, 11.76))
# first group_by the "per company, profile_id" variables and then apply rank()
library(dplyr)##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
df %>% group_by(company, profile_id) %>% mutate(rank = rank(price))## # A tibble: 6 x 6
## # Groups: company, profile_id [2]
## id profile_id company product price rank
## <dbl> <dbl> <fct> <fct> <dbl> <dbl>
## 1 1 1 A book 10.4 2
## 2 2 1 A shirt 23.9 3
## 3 3 1 A cup 5.95 1
## 4 4 2 B book 7.99 2
## 5 5 2 B shirt 5.95 1
## 6 6 2 B cup 11.8 3
cl <- read.table(text="
teamID X3M TR AS ST BK PTS FGP FTP
1 423 2884 1405 585 344 5797 0.4763141 0.7370821
2 467 2509 868 326 200 6159 0.4590164 0.7604167
3 769 1944 1446 614 168 6801 0.4248021 0.7825521
4 814 2457 1596 620 308 8058 0.4348856 0.8241445
5 356 2215 1153 403 243 4801 0.4427576 0.7478921
6 302 3360 1151 381 393 6271 0.4626974 0.6757176
7 384 2318 1070 431 269 5225 0.4345146 0.7460317
8 353 2529 1683 561 203 6150 0.4537273 0.7344740
9 598 2384 1635 497 162 6439 0.4512104 0.7998392
10 502 3191 1898 525 337 7107 0.4598565 0.7836970", header=T)
new <- cbind(cl$teamID, sapply(cl[,c(2:9)], rank))test <- data.frame(
V1 = c(11,22,44),
V2 = c(21,12,22),
V3 = c(35,66,12))
data.frame(test, t(apply(-test, 1, rank, ties.method='min')))## V1 V2 V3 V1.1 V2.1 V3.1
## 1 11 21 35 3 2 1
## 2 22 12 66 2 3 1
## 3 44 22 12 1 2 3
df <- structure(list(Rank = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16), Year = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("2001", "2003", "2005", "2007", "2009", "2011", "2013", "2015", "2017"), class = "factor")), .Names = c("Rank", "Year"), row.names = c(NA, -44L), class = c("tbl_df", "tbl", "data.frame"))
df %>% group_by(Year) %>%
mutate(normrank = (Rank - min(Rank)) / (max(Rank)+min(Rank)) )## # A tibble: 44 x 3
## # Groups: Year [3]
## Rank Year normrank
## <dbl> <fct> <dbl>
## 1 1 2001 0
## 2 2 2001 0.0556
## 3 3 2001 0.111
## 4 4 2001 0.167
## 5 5 2001 0.222
## 6 6 2001 0.278
## 7 7 2001 0.333
## 8 8 2001 0.389
## 9 9 2001 0.444
## 10 10 2001 0.5
## # ... with 34 more rows