Turns an observation into a variable in R

Asked

Viewed 26 times

1

I would like to know how to transpose the observations of a dataframe to turn it into variables.

The Subcategory column has to break down into several variables and its observations would be the values of the Value column.

 Subcategory           Month Year   Value
 Market Capitalisation  Jan 2017    4263898536
 Market Capitalisation  Feb 2017    4419020321
 Market Capitalisation  Mar 2017    4414519507
 Market Capitalisation  Apr 2017    4357532117
 Market Capitalisation  May 2017    4319919895
 Market Capitalisation  Jun 2017    4516233772
 Market Capitalisation  Jul 2017    4681045125
 Market Capitalisation  Aug 2017    4903344904
 Market Capitalisation  Sep 2017    4990670387
 Market Capitalisation  Oct 2017    5055295743
 Market Capitalisation  Nov 2017    4984089607
 Market Capitalisation  Dec 2017    5024983764
 Stock Market Index     Jan 2017    1106898267
 Stock Market Index     Feb 2017    113341555
 Stock Market Index     Mar 2017    1143731232
 Stock Market Index     Apr 2017    1134052782
 Stock Market Index     May 2017    1092165111
 Stock Market Index     Jun 2017    1110235884
 Stock Market Index     Jul 2017    1137200988
 Stock Market Index     Aug 2017    1160249513
 Stock Market Index     Sep 2017    1186970461
 Stock Market Index     Oct 2017    1195333521

Example of how it would look, remembering that I have data from 2017 until 2021.

   Market Capitalisation    Month   Year
   4263898536                Jan    2017
   4419020321                Feb    2017
   4414519507                Mar    2017
   4357532117                Apr    2017
   4319919895                May    2017
   4516233772                Jun    2017
   4681045125                Jul    2017
   4903344904                Aug    2017
  • See this topic for different options on how to do this: https://answall.com/questions/360773/o-que-s%C3%a3o-dados-no-formato-wide-long

1 answer

3


To reformat data from long to wide format, you can use the tidyverse:

library(dplyr)
library(tidyr)

dados %>%
  pivot_wider(
    id_cols = c(Year, Month),
    names_from = Subcategory,
    values_from = Value
  )

Dice

dados <-
structure(list(Subcategory = c("Market Capitalisation", "Market Capitalisation", 
"Market Capitalisation", "Market Capitalisation", "Market Capitalisation", 
"Market Capitalisation", "Market Capitalisation", "Market Capitalisation", 
"Market Capitalisation", "Market Capitalisation", "Market Capitalisation", 
"Market Capitalisation", "Stock Market Index", "Stock Market Index", 
"Stock Market Index", "Stock Market Index", "Stock Market Index", 
"Stock Market Index", "Stock Market Index", "Stock Market Index", 
"Stock Market Index", "Stock Market Index"), Month = c("Jan", 
"Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", 
"Nov", "Dec", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", 
"Aug", "Sep", "Oct"), Year = c("2017", "2017", "2017", "2017", 
"2017", "2017", "2017", "2017", "2017", "2017", "2017", "2017", 
"2017", "2017", "2017", "2017", "2017", "2017", "2017", "2017", 
"2017", "2017"), Value = c("4263898536", "4419020321", "4414519507", 
"4357532117", "4319919895", "4516233772", "4681045125", "4903344904", 
"4990670387", "5055295743", "4984089607", "5024983764", "1106898267", 
"113341555", "1143731232", "1134052782", "1092165111", "1110235884", 
"1137200988", "1160249513", "1186970461", "1195333521")), row.names = c(NA, 
-22L), class = "data.frame")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.