0

I have noticed an issue with the rounding in spread() (and I assume gather()). I have re-created the issue with some dummy data (below). What happens, is that when using spread() with doubles of more than 4 decimal places, the output of the spread has only 3 decimal places.

If anyone can shed some light on this that would be very helpful since I need to retain the 4 decimal place accuracy.

# Loading packages
library(tidyverse)

# Creating a dummy data set.
dummy_data <- tibble(
  day_of_week = c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday"),
  person = c("Jack", "Bob", "Bob", "Simon", "Simon"),
  value = c(0.2346, 0.7635, 0.7253, 0.7356, 0.1693)
)

# Spreading the data.
spread_data = dummy_data %>%
  spread(person, value)
FitzKaos
  • 381
  • 3
  • 20
  • 1
    I'm not able to duplicate your problem. When I `print(spead_data)` it looks like you are right, but when I `View(spead_data)` I see all of the decimal places in the spread data. – Adam Sampson Nov 14 '18 at 14:20
  • 2
    the precision is still there: `spead_data$Simon` shows it. I don't know to print the tibble with more precision. `as.data.frame(spead_data)` also shows more precision. – Ben Bolker Nov 14 '18 at 14:21
  • Interesting. Right ok, so I am then displaying this using DT package (within a shiny dashboard), so perhaps the solution lies in there? The `print(spread_data)` might be truncating the appearance but not the underlying value. I will investigate further. Thanks @AdamSampson and @bbolker – FitzKaos Nov 14 '18 at 14:25
  • 1
    Yep. Check out the other stackoverflow on that problem and change the number of digits to what you need: https://stackoverflow.com/questions/31022331/how-to-always-display-3-decimal-places-in-datatables-in-r-shiny – Adam Sampson Nov 14 '18 at 14:30

3 Answers3

1

I recreated the dummy variables in my R envir.

Indeed when print(spead_data), i get:

    day_of_week    Bob   Jack  Simon
  <chr>        <dbl>  <dbl>  <dbl>
1 Friday      NA     NA      0.169
2 Monday      NA      0.235 NA    
3 Thursday    NA     NA      0.736
4 Tuesday      0.764 NA     NA    
5 Wednesday    0.725 NA     NA   

However, if you access values directly, for example spead_data$Bob yields :

[1]     NA     NA     NA 0.7635 0.7253

Here are your 4 digits ! They never left, just the print function of tibbles that is a bit different.

I don't recommend turning your values to factors as @saisaran suggests, you won't be able to use them properly afterwards.


Edit : if you use print.data.frame(spead_data) instead of print(spead_data), you will get the output you need :

  day_of_week    Bob   Jack  Simon
1      Friday     NA     NA 0.1693
2      Monday     NA 0.2346     NA
3    Thursday     NA     NA 0.7356
4     Tuesday 0.7635     NA     NA
5   Wednesday 0.7253     NA     NA 

Source : https://community.rstudio.com/t/why-do-tibbles-and-data-frames-display-decimal-places-a-bit-differently/5722

Felix M
  • 122
  • 5
0

Does this work for you?

require(reshape2)
dummy_data %>%
  melt(id.vars=c("person","day_of_week")) %>% 
  dcast(value+day_of_week~person) %>% 
  select(-value)

You have several NAs but here is your result:

day_of_week    Bob   Jack  Simon
1      Friday     NA     NA 0.1693
2      Monday     NA 0.2346     NA
3   Wednesday 0.7253     NA     NA
4    Thursday     NA     NA 0.7356
5     Tuesday 0.7635     NA     NA
NelsonGon
  • 13,015
  • 7
  • 27
  • 57
0

problem with the data type and i was changed the data type :

dummy_data$value<-as.factor(dummy_data$value)
# Spreading the data.
spead_data = dummy_data %>%
  spread(person, value)       

OUTPUT:

# A tibble: 5 x 4
  day_of_week Bob    Jack   Simon 
  <chr>       <fct>  <fct>  <fct> 
1 Friday      NA     NA     0.1693
2 Monday      NA     0.2346 NA    
3 Thursday    NA     NA     0.7356
4 Tuesday     0.7635 NA     NA    
5 Wednesday   0.7253 NA     NA   

Note : Be cautious with the factor type data while using in any kind

sai saran
  • 737
  • 9
  • 32
  • Thanks! I am not sure factors will be workable with the real data I have, but I will check. – FitzKaos Nov 14 '18 at 14:26
  • It looks like this is not needed, but rather the issue in in the `print()` function and the numbers are still in 4 decimal places. Thanks for your input though! – FitzKaos Nov 14 '18 at 14:35