问题
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)
Desired Output
D_Longer <- data.frame(Stations = c("A","S","D","Z"), Value = c(15,10,25,16))
回答1:
Easiest would be stack
from base R
stack(D_Wider)[2:1]
Or using pivot_longer
library(tidyr)
pivot_longer(D_Wider, cols = everything(),
names_to = 'Stations', values_to = 'Value')
来源:https://stackoverflow.com/questions/63024920/how-to-reshape-a-wider-data-frame-to-longer-data-frame-in-r