首页 > 解决方案 > 导入 .csv 避免在 R 中转换为双精度数据

问题描述

我在我的数据框上使用包的descdist()功能时遇到问题。fitdistrplus我认为问题来自数据类型:double。我想避免在导入我的 csv 时转换为双精度,但将数据保留为数字(我显然无法使用 as.numeric 将它们转换回来,之后它仍然是双精度)。

这是我导入数据集的代码:

setwd("[directory]")
data=read.csv('data_BehCoor.csv', header=T, sep=";", dec=".", fill=T)

require("fitdistrplus")
descdist(data$stateTSp)

返回以下错误

Error in plot.window(...) : 'xlim' needs finite values

数据思路:

dput(head(data))
structure(list(day = c(2L, 2L, 2L, 2L, 2L, 2L), 
trial = c(1L, 1L, 1L, 1L, 1L, 1L), ID = structure(c(2L, 2L, 3L, 3L, 4L, 4L), 
.Label = c("", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"),
 class = "factor"), 
condition = structure(c(3L, 3L, 3L, 3L, 2L, 2L), .Label = c("", 
"C", "T"), class = "factor"), gender = structure(c(3L, 2L, 
3L, 2L, 3L, 2L), .Label = c("", "F", "M"), class = "factor"), 
TSp = c(5, 3, 1, 5, 0, 6), AGR = c(3, 0, 0, 0, 0, 0), beAGR = c(0, 
3, 0, 0, 0, 0), FOR = c(27.729, 24.51, 51.459, 37.645, 34.489, 
34.281), FOR_noTSp = c(22.729, 21.51, 50.459, 32.645, 34.489, 
28.281), NI = c(39.857, 82.421, 76.922, 9.277, 265.484, 249.692
), stateTSp = c(55.858, 21.607, 0, 79.961, 0, 2.001), TSpFOR = c(20.345, 
8.408, 0, 0, 0, 0), tot_duration = c(136.967, 136.967, 128.395, 
128.395, 300, 300), OL_FOR = c(3.746, 3.746, 5.002, 5.002, 
10.081, 10.081), OL_FOR_stateTSp = c(4.563, 10.907, 41.703, 
0, 0, 0), OL_FOR_TSpFOR = c(3.372, 1.113, 0, 0, 0, 0), OL_FOR_NI = c(11.041, 
8.496, 2.748, 27.639, 19.655, 18.191), OL_stateTSp_FOR = c(10.907, 
4.563, 0, 41.703, 0, 0), OL_stateTSp = c(3.249, 3.249, 0, 
0, 0, 0), OL_stateTSp_TSpFOR = c(4.034, 0, 0, 0, 0, 0),
OL_stateTSp_NI = c(36.66, 
11.79, 0, 37.249, 0, 2.001), OL_TSpFOR_FOR = c(1.113, 3.372, 
0, 0, 0, 0), OL_TSpFOR_stateTSp = c(0, 4.034, 0, 0, 0, 0), 
OL_TSpFOR = c(0, 0, 0, 0, 0, 0), OL_TSpFOR_NI = c(18.23, 
2.499, 0, 0, 0, 0), overlap_NI_FOR = c(8.496, 11.041, 27.639, 
2.748, 18.191, 19.655), OL_NI_stateTSp = c(11.79, 36.66, 
37.249, 0, 2.001, 0), OL_NI_TSpFOR = c(2.499, 18.23, 0, 0, 
0, 0), OL_NI = c(16.065, 16.065, 6.528, 6.528, 230.021, 230.021
), AGR_in_FOR = c(0, 0, 0, 0, 0, 0), AGR_in_stateTSp = c(0, 
0, 0, 0, 0, 0), AGR_in_TSpFOR = c(0, 0, 0, 0, 0, 0), AGR_in_NI = c(3, 
0, 0, 0, 0, 0), beAGR_in_FOR = c(0, 0, 0, 0, 0, 0), beAGR_in_stateTSp = c(0, 
0, 0, 0, 0, 0), beAGR_in_TSpFOR = c(0, 0, 0, 0, 0, 0), beAGR_in_NI = c(0, 
3, 0, 0, 0, 0), comment = structure(c(1L, 1L, 1L, 1L, 1L, 
1L), 
.Label = c("", "moved the plate too fast"), class = "factor")),
 row.names = c(NA, 6L), class = "data.frame")

提前致谢

标签: r

解决方案


fitdistrplus::descdist与 type 一起工作正常double,见下文:

foo <- runif(50, min = 1, max = 100)
typeof(foo)
fitdistrplus::descdist(foo)

推荐阅读