R 将行重新排序为多个值
我正在尝试安排我当前的数据集,以便为所有个人安排所有访问。 我尝试了所建议的方法,它有效,但只显示第一个个体的值 数据: 尝试的代码:R 将行重新排序为多个值,r,R,我正在尝试安排我当前的数据集,以便为所有个人安排所有访问。 我尝试了所建议的方法,它有效,但只显示第一个个体的值 数据: 尝试的代码: target <- c("First","Review","Last") Visit <- Visit[match(target, Visit$Visit),] target我认为您需要将Visit字段转换为带有排序的因子字段 target <- c("First"
target <- c("First","Review","Last")
Visit <- Visit[match(target, Visit$Visit),]
target我认为您需要将Visit
字段转换为带有排序的因子
字段
target <- c("First","Review","Last")
df$Visit <- factor(df$Visit, levels = target, ordered = T)
dplyr::arrange(df, Individual, Visit)
> dplyr::arrange(df, Individual, Visit)
# A tibble: 9 x 3
Individual Visit Amount
<chr> <ord> <dbl>
1 Anna First 100
2 Anna Review 75
3 Anna Last 25
4 John First 100
5 John Review 75
6 John Last 25
7 Seth First 100
8 Seth Review 75
9 Seth Last 25
我认为您需要将Visit
字段转换为带有排序的因子
字段
target <- c("First","Review","Last")
df$Visit <- factor(df$Visit, levels = target, ordered = T)
dplyr::arrange(df, Individual, Visit)
> dplyr::arrange(df, Individual, Visit)
# A tibble: 9 x 3
Individual Visit Amount
<chr> <ord> <dbl>
1 Anna First 100
2 Anna Review 75
3 Anna Last 25
4 John First 100
5 John Review 75
6 John Last 25
7 Seth First 100
8 Seth Review 75
9 Seth Last 25
您可以使用:
Visit[with(Visit, order(Individual, match(Visit, target))), ]
或使用dplyr
library(dplyr)
df %>% arrange(Individual, match(Visit, target))
# Individual Visit Amount
# <chr> <chr> <dbl>
#1 Anna First 100
#2 Anna Review 75
#3 Anna Last 25
#4 John First 100
#5 John Review 75
#6 John Last 25
#7 Seth First 100
#8 Seth Review 75
#9 Seth Last 25
库(dplyr)
df%>%安排(个人、比赛(参观、目标))
#个人访问量
#
#1安娜第一100
#2安娜评论75
#3安娜去年25岁
#约翰第一100
#5约翰评论75
#约翰去年25岁
#7赛斯第一100
#8赛斯评论75
#9赛斯最后25
您可以使用:
Visit[with(Visit, order(Individual, match(Visit, target))), ]
或使用dplyr
library(dplyr)
df %>% arrange(Individual, match(Visit, target))
# Individual Visit Amount
# <chr> <chr> <dbl>
#1 Anna First 100
#2 Anna Review 75
#3 Anna Last 25
#4 John First 100
#5 John Review 75
#6 John Last 25
#7 Seth First 100
#8 Seth Review 75
#9 Seth Last 25
库(dplyr)
df%>%安排(个人、比赛(参观、目标))
#个人访问量
#
#1安娜第一100
#2安娜评论75
#3安娜去年25岁
#约翰第一100
#5约翰评论75
#约翰去年25岁
#7赛斯第一100
#8赛斯评论75
#9赛斯最后25