[英]Using tidyverse in R how do I arrange my column values in a fixed way?
ID score
a 1
a 2
b 2
b 4
c 4
c 5
I want to change id to "a,b,c" order this to我想将 id 更改为 "a,b,c" 命令
ID score
a 1
b 2
c 4
a 2
b 4
c 5
What I tried我试过的
> data <- read_csv(data)
> data <- factor(data$id, levels = c('a', 'b', 'c'))
This works for tables so I tried it but didn't work for this.这适用于表格,所以我尝试过,但没有为此工作。 Anybody know if there is a way?
有人知道有办法吗?
Instead of assigning the 'id' column to data <-
(which would replace the data with the 'id' values) it would be used for order
ing.它不是将 'id' 列分配给
data <-
(这将用 'id' 值替换数据),而是用于order
。 In base R
, this can be done with在
base R
,这可以通过
data1 <- data[order(duplicated(data$ID)),]
row.name(data1) <- NULL
Or with dplyr
或者用
dplyr
library(dplyr)
library(data.table)
data %>%
arrange(rowid(ID))
library(dplyr)
d %>%
group_by(ID) %>%
mutate(r = row_number()) %>%
ungroup() %>%
arrange(r, ID, score) %>%
select(-r)
OR in base R或在基础 R
with(d, d[order(ave(seq(NROW(d)), d$ID, FUN = seq_along), ID, score),])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.