本文介绍了如何在 R 中将更宽的 data.frame 重塑为更长的 data.frame?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在玩 pivot_longer
和 pivot_wider
但可能遗漏了一些东西.我有一个 data.frame
像 D_Wider
并且想把它转换成类似 D_longer
的东西.有什么办法吗?
I was playing with pivot_longer
and pivot_wider
but probably am missing something. I have a data.frame
like D_Wider
and would like to convert it to something like D_longer
. any way forward?
library(tidyverse)
D_Wider <- data.frame(A = 15, S = 10, D = 25, Z = 16)
期望输出
D_Longer <- data.frame(Stations = c("A","S","D","Z"), Value = c(15,10,25,16))
推荐答案
最简单的是 stack
from base R
Easiest would be stack
from base R
stack(D_Wider)[2:1]
或者使用pivot_longer
library(tidyr)
pivot_longer(D_Wider, cols = everything(),
names_to = 'Stations', values_to = 'Value')
这篇关于如何在 R 中将更宽的 data.frame 重塑为更长的 data.frame?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!