The pivot_longer() function from tidyr package is used to pivot dataframe from wide format to a long format.
The following method shows how you can do it with syntax.
Method 1: Use pivot_longer() Function
pivot_longer(df, cols = everything())
The following example shows how to use pivot_longer() function in R to create dataframe from wide format to long format.
Using pivot_longer() Function
Let’s see how we can use pivot_longer() Function in R:
# Create dataframe
df <- data.frame(Pressure1=c(12,11,12,13,13,11),
Pressure2=c(78,89,85,84,81,79))
# Create pivot table
df_long <- pivot_longer(df, cols = everything())
# Print pivot table
print(df_long)
Output:
name value
<chr> <dbl>
1 Pressure1 12
2 Pressure2 78
3 Pressure1 11
4 Pressure2 89
5 Pressure1 12
6 Pressure2 85
7 Pressure1 13
8 Pressure2 84
9 Pressure1 13
10 Pressure2 81
11 Pressure1 11
12 Pressure2 79
Here the output shows longer format of dataframe whicb created using pivot_longer() function in R.