R Tidyverse spread()函数多个小数位截断问题

时间:2018-11-14 14:16:47

标签: r rounding tidyverse

我注意到spread()中的四舍五入有一个问题(我假设为gather())。我用一些伪数据重新创建了问题(如下)。发生的情况是,当使用spread()时,双精度数超过4个小数位时,价差的输出只有3个小数位。

如果有人可以阐明这一点,那将非常有帮助,因为我需要保留小数点后四位的精度。

# 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)

3 个答案:

答案 0 :(得分:1)

我在R环境中重新创建了虚拟变量。

实际上print(spead_data)出现时,我得到了:

    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   

但是,如果直接访问值,例如spead_data$Bob会产生:

[1]     NA     NA     NA 0.7635 0.7253

这是您的4位数字!他们再也没有离开过,只是小玩意的打印功能有所不同。

我不建议您像@saisaran所建议的那样将您的价值观转变为因素,之后您将无法正确使用它们。


编辑: 如果您使用print.data.frame(spead_data)而不是print(spead_data),则会得到所需的输出:

  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 

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

答案 1 :(得分:0)

这对您有用吗?

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

您有多个NA,但这是您的结果:

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

答案 2 :(得分:0)

数据类型存在问题,我被更改为数据类型:

plugins {
    id 'java'
}

group 'cucumber_cash_withdrawal'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    testCompile 'io.cucumber:cucumber-java:2.4.0'
    testCompile 'io.cucumber:cucumber-junit:2.4.0'
    testCompile group: 'info.cukes', name: 'cucumber-picocontainer', version: '1.2.5'
    implementation group: 'org.eclipse.jetty', name: 'jetty-webapp', version: '9.4.12.v20180830'
    implementation group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.141.5'
}

configurations {
    cucumberRuntime {
        extendsFrom testRuntime
    }
}

task cucumber() {
    dependsOn assemble, compileTestJava
    doLast {
        javaexec {
            main = "cucumber.api.cli.Main"
            classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
            args = ['--plugin', 'pretty', '--glue', 'gradle.cucumber', 'src/test/resources']
        }
    }
}

apply plugin: 'java'

task runApp(type: JavaExec) {
    classpath = sourceSets.main.runtimeClasspath
    main = 'cash_withdrawal.AtmServer'
}

输出:

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

注意:在使用任何一种类型时,请谨慎使用因子类型数据