将2列放入列表并在R中排序
假设我们有两个列表将2列放入列表并在R中排序,r,list,sorting,R,List,Sorting,假设我们有两个列表 x <- c(1, 3, 4, 2, 6, 5) y <- c(12, 14, 15, 61, 71, 21) 在你有了一个列表之后,我想在y上对它进行排序,这样最终的列表看起来像 x <- c(1, 3, 4, 5, 2, 6) y <- c(12, 14, 15, 21, 61, 71) 所以我想知道是否有人能帮我。您需要先将它们放在数据框中,然后使用顺序: x <- c(1, 3, 4, 2, 6, 5) y <- c(-12,
x <- c(1, 3, 4, 2, 6, 5)
y <- c(12, 14, 15, 61, 71, 21)
在你有了一个列表之后,我想在y
上对它进行排序,这样最终的列表看起来像
x <- c(1, 3, 4, 5, 2, 6)
y <- c(12, 14, 15, 21, 61, 71)
所以我想知道是否有人能帮我。您需要先将它们放在
数据框中,然后使用顺序
:
x <- c(1, 3, 4, 2, 6, 5)
y <- c(-12, 14, 15, 61, 71, 21)
DF <- data.frame(x, y)
> DF[order(DF$y),]
x y
1 1 -12
2 3 14
3 4 15
6 5 21
4 2 61
5 6 71
x使用lappy
作为列表保存:
x <- c(1, 3, 4, 2,6,5)
y <- c(12, 14,15,61,71,21)
l <- list(x = x, y = y)
## thelatemail
lapply(l, `[`, order(l$y))
# $x
# [1] 1 3 4 5 2 6
#
# $y
# [1] 12 14 15 21 61 71
rapply(l, function(x) x[order(l$y)], how = 'list')
# $x
# [1] 1 3 4 5 2 6
#
# $y
# [1] 12 14 15 21 61 71
或rappy
:
x <- c(1, 3, 4, 2,6,5)
y <- c(12, 14,15,61,71,21)
l <- list(x = x, y = y)
## thelatemail
lapply(l, `[`, order(l$y))
# $x
# [1] 1 3 4 5 2 6
#
# $y
# [1] 12 14 15 21 61 71
rapply(l, function(x) x[order(l$y)], how = 'list')
# $x
# [1] 1 3 4 5 2 6
#
# $y
# [1] 12 14 15 21 61 71
lappy(l,`[`,order(l$y))
简而言之^^很有趣。很好,很懒
rapply(l, function(x) x[order(l$y)], how = 'list')
# $x
# [1] 1 3 4 5 2 6
#
# $y
# [1] 12 14 15 21 61 71