我有这张桌子:
head(tb)
shot_made_flag
opponent 0 1
ATL 54.79452 45.20548
BKN 60.00000 40.00000
BOS 58.87612 41.12388
CHA 56.40000 43.60000
CHI 56.97674 43.02326
CLE 56.03113 43.96887
这是我的代码:
table(opponent,shot_made_flag)
rowSums(table(opponent,shot_made_flag))
b<table(opponent,shot_made_flag)/rowSums(table(opponent,shot_made_flag))*100
head(tb)
我想按照第1列中的值对表格进行排序,但我从来没有得到过表格。我只获得第1列中按月新月(或下降顺序)排序的值
答案 0 :(得分:0)
使用order
函数和列前的-
(减号)符号按降序排列,如果希望Result
按升序排列,则不使用减号。
df <- read.table(header=T, text="
opponent 0 1
ATL 54.79452 45.20548
BKN 60.00000 40.00000
BOS 58.87612 41.12388
CHA 56.40000 43.60000
CHI 56.97674 43.02326
CLE 56.03113 43.96887
",check.names=F)
df
# opponent 0 1
# 1 ATL 54.79452 45.20548
# 2 BKN 60.00000 40.00000
# 3 BOS 58.87612 41.12388
# 4 CHA 56.40000 43.60000
# 5 CHI 56.97674 43.02326
# 6 CLE 56.03113 43.96887
names(df)
# [1] "opponent" "0" "1"
Result <- df[order(-df$"1"),]
Result
# opponent 0 1
# 1 ATL 54.79452 45.20548
# 6 CLE 56.03113 43.96887
# 4 CHA 56.40000 43.60000
# 5 CHI 56.97674 43.02326
# 3 BOS 58.87612 41.12388
# 2 BKN 60.00000 40.00000
# Alternative
Result <- df[order(-df[,3]),]
Result
# opponent 0 1
# 1 ATL 54.79452 45.20548
# 6 CLE 56.03113 43.96887
# 4 CHA 56.40000 43.60000
# 5 CHI 56.97674 43.02326
# 3 BOS 58.87612 41.12388
# 2 BKN 60.00000 40.00000