• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Rstats 101

Learn R Programming Tips & Tricks for Statistics and Data Science

  • Home
  • About
    • Privacy Policy
  • Show Search
Hide Search

pivot_longer on dataframe with single row

rstats101 · October 30, 2022 ·

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

Related

Filed Under: pivot_longer(), tidyr Tagged With: pivot_longer() all columns, tidyr pivot_longer()

Primary Sidebar

Recent Posts

  • How to create a nested dataframe with lists
  • How to compute proportion with tidyverse
  • How to Compute Z-Score of Multiple Columns
  • How to drop unused level of factor variable in R
  • How to compute Z-score

Categories

%in% arrange() as.data.frame as_tibble built-in data R colSums() R cor() in R data.frame dplyr dplyr across() dplyr group_by() dplyr rename() dplyr rowwise() dplyr row_number() dplyr select() dplyr slice_max() dplyr slice_sample() drop_na R duplicated() gsub head() impute with mean values is.element() linear regression matrix() function na.omit R NAs in R near() R openxlsx pivot_longer() prod() R.version replace NA replace NAs tidyverse R Function rstats rstats101 R version scale() sessionInfo() t.test() tidyr tidyselect tidyverse write.xlsx

Copyright © 2025 · Daily Dish Pro on Genesis Framework · WordPress · Log in

Go to mobile version