我想在循环中更改每个输出表中行的顺序。
我目前正在将lapply
与dcast
一起使用,以获取一组表,并且我尝试在列表中设置所需的顺序,然后在列表中使用{ {1}}。
这是我正在尝试的:
match()
但是,我希望在表格的末尾具有“ Hornet 4 Drive”模型,如下所示:
#Setup data
mtcars1 <- head(mtcars, 10)
mtcars1$model <- rownames(mtcars1)
df <- dplyr::bind_rows(mtcars1, mtcars1, mtcars1)
df$year <- c(1975, 1983)
df$class <- c(2,2,2,3,3,3,4,4,4,4,2,2,2,3,3)
#Create set of tables
tables <- lapply(split(df, df$cyl),
FUN = function(d) reshape2::dcast(model + class ~ year,
value.var = "year", length , data = df))
tables
#My output has the rows arranged according to model alphabetical order, like this:
model class 1975 1983
1 Datsun 710 2 2 0
2 Datsun 710 4 1 0
3 Duster 360 2 2 0
4 Duster 360 4 1 0
5 Hornet 4 Drive 3 0 2
6 Hornet 4 Drive 4 0 1
7 Hornet Sportabout 3 2 0
8 Hornet Sportabout 4 1 0
9 Mazda RX4 2 2 0
10 Mazda RX4 3 1 0
11 Mazda RX4 Wag 2 0 2
12 Mazda RX4 Wag 4 0 1
13 Merc 230 3 2 0
14 Merc 230 4 1 0
15 Merc 240D 2 0 2
16 Merc 240D 4 0 1
17 Merc 280 3 0 2
18 Merc 280 4 0 1
19 Valiant 2 0 2
20 Valiant 3 0 1
我目前正在尝试的是:
model class 1975 1983
1 Datsun 710 2 2 0
2 Datsun 710 4 1 0
3 Duster 360 2 2 0
4 Duster 360 4 1 0
7 Hornet Sportabout 3 2 0
8 Hornet Sportabout 4 1 0
9 Mazda RX4 2 2 0
10 Mazda RX4 3 1 0
11 Mazda RX4 Wag 2 0 2
12 Mazda RX4 Wag 4 0 1
13 Merc 230 3 2 0
14 Merc 230 4 1 0
15 Merc 240D 2 0 2
16 Merc 240D 4 0 1
17 Merc 280 3 0 2
18 Merc 280 4 0 1
19 Valiant 2 0 2
20 Valiant 3 0 1
5 Hornet 4 Drive 3 0 2
6 Hornet 4 Drive 4 0 1
上面的输出未包含所有行,但在“模型”列中删除了所有重复的名称。
有人对如何解决这个问题有想法吗? 预先感谢您的帮助!
答案 0 :(得分:0)
如果我理解正确,那么所有的问题是您“希望在表的末尾拥有“ Hornet 4 Drive”模型”。这是一个示例,您可以怎么做:
df <- data.frame(num= 1:8, name= c("a", "a", "b", "b", "b", "c", "c", "d"), stringsAsFactors = FALSE)
name_last <- "b"
df <- rbind.data.frame(df[df$name != name_last, ], df[df$name == name_last, ])
df
num name
1 1 a
2 2 a
6 6 c
7 7 c
8 8 d
3 3 b
4 4 b
5 5 b
答案 1 :(得分:0)
这应该是您要寻找的:
tables_reordered<-lapply(tables, function (x) {rbind(x[which(!x$model=="Hornet 4 Drive"),], x[which(x$model=="Hornet 4 Drive"),])})
答案 2 :(得分:0)
或者,可以通过强制model
分解因子,使用forcats::fct_relevel()
重新排列因子水平以及按因子水平排序来解决此问题。
lapply(tables, function(x) {
x[order(forcats::fct_relevel(x$model, "Hornet 4 Drive", after = Inf)), ]
})
$`4` model class 1975 1983 1 Datsun 710 2 2 0 2 Datsun 710 4 1 0 3 Duster 360 2 2 0 4 Duster 360 4 1 0 7 Hornet Sportabout 3 2 0 8 Hornet Sportabout 4 1 0 9 Mazda RX4 2 2 0 10 Mazda RX4 3 1 0 11 Mazda RX4 Wag 2 0 2 12 Mazda RX4 Wag 4 0 1 13 Merc 230 3 2 0 14 Merc 230 4 1 0 15 Merc 240D 2 0 2 16 Merc 240D 4 0 1 17 Merc 280 3 0 2 18 Merc 280 4 0 1 19 Valiant 2 0 2 20 Valiant 3 0 1 5 Hornet 4 Drive 3 0 2 6 Hornet 4 Drive 4 0 1 $`6` model class 1975 1983 1 Datsun 710 2 2 0 2 Datsun 710 4 1 0 3 Duster 360 2 2 0 4 Duster 360 4 1 0 7 Hornet Sportabout 3 2 0 8 Hornet Sportabout 4 1 0 9 Mazda RX4 2 2 0 10 Mazda RX4 3 1 0 11 Mazda RX4 Wag 2 0 2 12 Mazda RX4 Wag 4 0 1 13 Merc 230 3 2 0 14 Merc 230 4 1 0 15 Merc 240D 2 0 2 16 Merc 240D 4 0 1 17 Merc 280 3 0 2 18 Merc 280 4 0 1 19 Valiant 2 0 2 20 Valiant 3 0 1 5 Hornet 4 Drive 3 0 2 6 Hornet 4 Drive 4 0 1 $`8` model class 1975 1983 1 Datsun 710 2 2 0 2 Datsun 710 4 1 0 3 Duster 360 2 2 0 4 Duster 360 4 1 0 7 Hornet Sportabout 3 2 0 8 Hornet Sportabout 4 1 0 9 Mazda RX4 2 2 0 10 Mazda RX4 3 1 0 11 Mazda RX4 Wag 2 0 2 12 Mazda RX4 Wag 4 0 1 13 Merc 230 3 2 0 14 Merc 230 4 1 0 15 Merc 240D 2 0 2 16 Merc 240D 4 0 1 17 Merc 280 3 0 2 18 Merc 280 4 0 1 19 Valiant 2 0 2 20 Valiant 3 0 1 5 Hornet 4 Drive 3 0 2 6 Hornet 4 Drive 4 0 1