- 已编辑
问题
有一组数据:
data <- structure(list(longten = c(37.45, 42, 1300.6, 288.8, 157.05,
487.4), tollten = c(0, 211.45, 1247.2, 0, 0, 798.4), equipten = c(0,
0, 0, 0, 0, 0), cardten = c(110, 125, 2150, 0, 0, 570), wireten = c(0,
380.35, 0, 0, 0, 0), longmon = c(3.7, 4.4, 18.15, 9.45, 6.3,
11.8), tollmon = c(0, 20.75, 18, 0, 0, 19.25), equipmon = c(0,
0, 0, 0, 0, 0), cardmon = c(7.5, 15.25, 30.25, 0, 0, 13.5), wiremon = c(0,
35.7, 0, 0, 0, 0)), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))
它们是由*mon
和*ten
两两对应的,想把这个数据框变形为:
| ten | mon | type |
| :-----: | :----: | :----: |
| 单元格 | 单元格 | long |
| 单元格 | 单元格 | toll |
...
笨办法
这是我用的笨办法,分别依赖于操作方式的一致才使得最后的cbind()
可以对应,但是我感觉这个方法不太好。
data_mon <- select(data, contains('mon')) %>%
pivot_longer(cols = contains('mon'),
names_to = 'type', names_pattern = '(.*)mon',
values_to = 'mon')
data_ten <- select(data, contains('ten')) %>%
pivot_longer(cols = contains('ten'),
names_to = 'type', names_pattern = '(.*)ten',
values_to = 'ten')
mon_ten <- cbind(data_mon, ten = data_ten$ten)
> mon_ten
type mon ten
1 long 3.70 37.45
2 toll 0.00 0.00
3 equip 0.00 0.00
4 card 7.50 110.00
5 wire 0.00 0.00
6 long 4.40 42.00
7 toll 20.75 211.45
8 equip 0.00 0.00
9 card 15.25 125.00
10 wire 35.70 380.35
11 long 18.15 1300.60
12 toll 18.00 1247.20
13 equip 0.00 0.00
14 card 30.25 2150.00
15 wire 0.00 0.00
16 long 9.45 288.80
17 toll 0.00 0.00
18 equip 0.00 0.00
19 card 0.00 0.00
20 wire 0.00 0.00
21 long 6.30 157.05
22 toll 0.00 0.00
23 equip 0.00 0.00
24 card 0.00 0.00
25 wire 0.00 0.00
26 long 11.80 487.40
27 toll 19.25 798.40
28 equip 0.00 0.00
29 card 13.50 570.00
30 wire 0.00 0.00
请问大家有无更加贴切的方法吗?