0

I have wide data which I want to make longer using pivot_longer(). However, the columns are of different types: some whole numbers, some decimals. On doing pivot_longer() all of them are transformed to the scientific notation (because of the values_transform parameter). Firstly, I don't want the scientific notation. Secondly, I want the various columns to retain their original types. I can't figure out what to feed the values_transform parameter to achieve this.

The data is just one row of multiple values, so I don't think it necessary to provide it here. To clarify, I don't intend to apply further operations on the table after pivoting, so the different types shouldn't be an issue. It is just for presentation of the results.

Edit

Example data:

> dput(x)
structure(list(a = 336, b = 7.5851965, c = 73396, d = 443516.25, 
    e = 379751.5, f = 29309.5, g = 386923.825, h = 6974.75, i = 9, 
    j = 158.75), class = "data.frame", row.names = c(NA, -1L))

Trying to pivot:

y <- x %>% 
  pivot_longer(everything(), names_to = "NAMES", values_to = "VALUES")

While making this edit I realised the tibble summary shows correct formatting, but in the RStudio viewer and as dataframe the formats are changed:

> y
# A tibble: 10 × 2
   NAMES    VALUES
   <chr>     <dbl>
 1 a        336   
 2 b          7.59
 3 c      73396   
 4 d     443516.  
 5 e     379752.  
 6 f      29310.  
 7 g     386924.  
 8 h       6975.  
 9 i          9   
10 j        159.  

> as.data.frame(y)
   NAMES       VALUES
1      a 3.360000e+02
2      b 7.585197e+00
3      c 7.339600e+04
4      d 4.435162e+05
5      e 3.797515e+05
6      f 2.930950e+04
7      g 3.869238e+05
8      h 6.974750e+03
9      i 9.000000e+00
10     j 1.587500e+02

I was confused by the RStudio viewer changing the formatting (it sbows x correctly), but why could this be so?

  • 6
    It's easier to help you if you make your question reproducible by including data in a useable format eg paste the output of `dput(your_data)` into the question to enable testing and verification of possible solutions. It’s helpful if you show the code have you tried to explicitly and unambiguously reveal problems. As well as the expected output. [Link for guidance on asking questions](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Peter Jan 29 '22 at 13:35
  • 1
    We would need the data! – TarJae Jan 29 '22 at 15:23
  • 1
    „ The data is just one row of multiple values, so I don't think it necessary to provide it here.“ It is very important to provide the data. And it will increase the likelihood to get an answer. – deschen Jan 29 '22 at 18:19
  • @Peter I've edited the post with data and code, and in doing so, my question has changed! I apologise for this, everyone. – Karthik Thrikkadeeri Jan 30 '22 at 01:32
  • 4
    The scientific notation is just printing behavior, it's a reflection of your print settings, not a change in the data. – Gregor Thomas Jan 30 '22 at 01:40
  • 1
    And you want all your numeric data in a single column. A single column can only have one class. So your integers must convert to numeric. – Gregor Thomas Jan 30 '22 at 01:41
  • 1
    And there are different print methods for tibbles and data frames. See `?print.data.frame` and `?print.tibble` to see the options available on each. – Gregor Thomas Jan 30 '22 at 01:42

1 Answers1

1

Not sure if this is helpful, but there is a quick solution to that using a quick insert of round and mutate:

as.data.frame(y) %>% 
  mutate(VALUES = round(VALUES))

This produces this data frame without the notation:

NAMES VALUES
1      a    336
2      b      8
3      c  73396
4      d 443516
5      e 379752
6      f  29310
7      g 386924
8      h   6975
9      i      9
10     j    159

And if you dont wanna lose any numbers to rounding, you can specify where to round:

as.data.frame(y) %>% 
  mutate(VALUES = round(VALUES, 2))

Which gives you this:

   NAMES    VALUES
1      a    336.00
2      b      7.59
3      c  73396.00
4      d 443516.25
5      e 379751.50
6      f  29309.50
7      g 386923.82
8      h   6974.75
9      i      9.00
10     j    158.75
Shawn Hemelstrand
  • 2,676
  • 4
  • 17
  • 30