使用否定符号作为gather
,-a.b
与a.b
执行代码,每个观察结果转换1行,每个变量转换1列:
res1 <- data.frame("a.b" = c(1,2), c = c(3,4), "d" = c(5,6))
res1 %>%
gather(key = k,
value = v,
-a.b)
res1 %>%
gather(key = k,
value = v,
a.b)
产生:
> res1 %>%
+ gather(key = k,
+ value = v,
+ -a.b)
a.b k v
1 1 c 3
2 2 c 4
3 1 d 5
4 2 d 6
>
> res1 %>%
+ gather(key = k,
+ value = v,
+ a.b)
c d k v
1 3 5 a.b 1
2 4 6 a.b 2
res1是:
> res1
a.b c d
1 1 3 5
2 2 4 6