我想对数据帧的行(30列)进行排名,其数值从-inf到+ inf排名。 这就是我所拥有的:
df <- structure(list(StockA = c("-5", "3", "6"),
StockB = c("2", "-1", "3"),
StockC = c("-3", "-4", "4")),
.Names = c( "StockA","StockB", "StockC"),
class = "data.frame", row.names = c(NA, -3L))
> df
StockA StockB StockC
1 -5 2 -3
2 3 -1 -4
3 6 3 4
这就是我想要的:
> df_rank
StockA StockB StockC
1 3 1 2
2 1 2 3
3 1 3 2
我正在使用此命令:
> rank(df[1,])
StockA StockB StockC
2 3 1
尽管如您所见,但得到的排名变量并不正确。
答案 0 :(得分:1)
rank()
将最低等级分配给最小值。
因此,对您的问题的简短回答是使用向量的等级乘以-1:
rank (-c(-5, 2, -3) )
[1] 1 3 2
以下是完整代码:
# data frame definition. The numbers should actually be integers as pointed out
# in comments, otherwise the rank command will sort them as strings
# So in the real word you should define them as integers,
# but to go with your data I will convert them to integers in the next step
df <- structure(list(StockA = c("-5", "3", "6"),
StockB = c("2", "-1", "3"),
StockC = c("-3", "-4", "4")),
.Names = c( "StockA","StockB", "StockC"),
class = "data.frame", row.names = c(NA, -3L))
# since you plan to rank them not as strings, but numbers, you need to convert
# them to integers:
df[] <- lapply(df,as.integer)
# apply will return a matrix or a list and you need to
# transpose the result and convert it back to a data.frame if needed
result <- as.data.frame(t( apply(df, 1, FUN=function(x){ return(rank(-x)) }) ))
result
# StockA StockB StockC
# 3 1 2
# 1 2 3
# 1 3 2