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?