使用秩的r数据帧
我想对一个数据帧(有30列)的行进行排序,它的数值从-inf到+inf。 这就是我所拥有的:使用秩的r数据帧,r,dataframe,rank,R,Dataframe,Rank,我想对一个数据帧(有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
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
我正在使用此命令:
> rank(df[1,])
StockA StockB StockC
2 3 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
#数据帧定义。正如所指出的,这些数字实际上应该是整数
#在注释中,否则rank命令将把它们作为字符串排序
#所以在现实生活中,你应该把它们定义为整数,
#但为了处理数据,我将在下一步将它们转换为整数
非常感谢你,卡蒂亚,非常感谢!!
# 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