我正在尝试对R中的列进行重新排序。这是这种情况:
我现在有9列:A-NP F-WR K-WR H-ER Q-ER B-NP C-NP Z-WR X-ER
规则是NP> WR> ER。因此,名称为NP的列应为第一列。在同一组的列中,按字母顺序排列。
因此所需的输出将是:
A-NP B-NP C-NP F-WR K-WR Z-WR H-ER Q-ER X-ER
这些列采用这种格式(something-NP / WR / ER),但是每次都不同,所以我想创建一些可以动态排序这些列的东西。
有什么想法吗?
谢谢
答案 0 :(得分:0)
# create an example data frame
df <- data.frame(matrix(ncol = 9, nrow = 0))
names(df) <- c("A-NP", "F-WR", "K-WR", "H-ER", "Q-ER", "B-NP", "C-NP", "Z-WR", "X-ER")
library(dplyr)
df %>%
# sort in alphabetical order
select(sort(names(.))) %>%
# sort based on the rule
select(contains("NP"), contains("WR"), contains("ER"))
# [1] A-NP B-NP C-NP F-WR K-WR Z-WR H-ER Q-ER X-ER
# <0 rows> (or 0-length row.names)
答案 1 :(得分:0)
这是使用df <- data.frame(matrix(ncol = 9, nrow = 0))
names(df) <- c("F-WR", "K-WR", "H-ER", "Q-ER", "B-NP", "C-NP", "Z-WR", "X-ER", "A-NP")
df[, c(sort(grep("NP", names(df), value = T)),
sort(grep("WR", names(df), value = T)),
sort(grep("ER", names(df), value = T))
)
]
[1] A-NP B-NP C-NP F-WR K-WR Z-WR H-ER Q-ER X-ER
<0 rows> (or 0-length row.names)
的基本R解决方案-
public function getFilters(): array
{
return [,
new TwigFilter('image', [$this, 'image'], ['is_safe' => ['html']]),
];
}