我试图在r中转换一段python代码,但是我不知道如何实现它。
在python中,我们可以做到:
## dictionary
a_list = {'red':23, 'black':12,'white':4,'orange':79}
## sort by key
dict(sorted(a_list.items()))
{'black': 12, 'orange': 79, 'red': 23, 'white': 4}
## sort by values
sorted(a_list.items(), key=lambda x: x[1])
[('white', 4), ('black', 12), ('red', 23), ('orange', 79)]
对于这个问题,我有一个:
a_list <- list(red=23, black=12, white = 4, orange=79)
我想用两种方式对这个列表进行排序,这样输出是:
输出1(按键排序):
list(black=12, orange=79, red=23, white = 4)
输出2(按值排序):
list(white = 4,black=12, red=23,orange=79)
我该怎么做?