我有一堆数字的字符向量,如下所示:
numbers_c <- c("8.76782130111354e-05", "0.1523", "0.4316", "6.80470451316959e-05","2.45",
"5.29226369075514e-05", "6.123", "7.11446903389845e-05")
我想把它们从大到小排序,所以我使用了内置的排序函数,得到了这个:
> sort(numbers_c, decreasing = TRUE)
[1] "8.76782130111354e-05" "7.11446903389845e-05" "6.80470451316959e-05"
[4] "6.123" "5.29226369075514e-05" "2.45"
[7] "0.4316" "0.1523"
排序函数似乎没有得到带"的数字;e-05";非常小。我怎样才能让它明白这一点?
您可以将字符转换为数字并使用order
。
numbers_c[order(as.numeric(numbers_c), decreasing = TRUE)]
#[1] "6.123" "2.45" "0.4316"
#[4] "0.1523" "8.76782130111354e-05" "7.11446903389845e-05"
#[7] "6.80470451316959e-05" "5.29226369075514e-05"