pivot_longer on dataframe with single row

In this tutorial, we will see an example of using pivot_longer() function to reshape, a dataframe with a single row, from wide form to long tidy form.

Sometimes you might have a dataframe with just one long row and it is useful to reshape into longer format. We will start with creating a dataframe with a single row. And then use pivot_longer() function to covert the wide dataframee to long dataframe. Note that this use case of converting a single row dataframe is not any different from a wide dataframe with many rows. It is one of the simple examples of pivot_longer()

Let us load tidyverse, the meta R package.

library(tidyverse)

We will use iris data, readily built-in within R, to create a wide dataframe with a single row.

iris %>% head()

  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa

If we compute column-wise summary statistics, like mean, on all numerical columns, we will get a dataframe with a single row. In the example below, we compute column-wise mean on all numerical columns using dplyr’s across() function. This would result in a dataframe with single row.

iris %>%
  summarise(across(where(is.numeric), mean))

Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
    5.843333     3.057333     3.758000     1.199333 

pivot_longer(): Reshape wide to long form

To reshape this wide data frame into long dataframe, we use pivot_longer() function with arguments specifying thee columns we need to reshape, variable names for column names and the values in the wide dataframe.

iris %>%
  summarise(across(where(is.numeric), mean)) %>%
  pivot_longer(cols=everything(), 
               names_to="feature", 
               values_to="mean_values")

# A tibble: 4 × 2
  feature      mean_values
  <chr>              <dbl>
1 Sepal.Length        5.84
2 Sepal.Width         3.06
3 Petal.Length        3.76
4 Petal.Width         1.20

In the above example, we reshape all the columns by selecting all the columns using everything() function. In case you missed specifying which columns to reshape, i.e. without the col argument wee will get the following error.

iris %>%
  summarise(across(is.numeric, mean)) %>%
  pivot_longer(names_to="feature", 
               values_to="mean_values")
 Error in `$<-.data.frame`(`*tmp*`, "call_text", value = c("iris %>% summarise(across(is.numeric, mean)) %>% ...", : 
replacement has 5 rows, data has 3
Exit mobile version