简体   繁体   English

如何确定数字是.txt中的数字?

[英]How do I make sure numbers are numeric from a .txt?

I'm setting up a script to extract the thickness and voltages from a single column text file and perform a Weibull distribution on it. 我正在设置一个脚本,以从单列文本文件中提取厚度和电压,并对其执行Weibull分布。 When I try to use fitdistr() I get an error stating " 'x' must be a non-empty numeric vector ". 当我尝试使用fitdistr() ,出现错误,指出“ 'x' must be a non-empty numeric vector ”。 R is supposed to interpret numbers in text files as numeric but that doesn't seem to be happening. R应该将文本文件中的数字解释为数字,但这似乎没有发生。 Any thoughts? 有什么想法吗?

filename <- "SampleBreakdownSet.txt"

d <- read.table(filename, header = FALSE, sep = "")

#Extract thickness from the dataset; set to variable t
t = d[1,1]

#Extract the breakdown voltages and toss into dataset, BDV
BDV = tail(d,(nrow(d)-1))

#Calculates the breakdown field from the thickness and BDV
BDF = (BDV*10000) / t

#Calculates the Weibull parameters from the input breakdown voltages.
fitdistr(BDF, densfun ="weibull", lower = 0)

fitdistr(BDF, densfun ="weibull", lower = 0) Error in fitdistr(BDF, densfun = "weibull", lower = 0) : 'x' must be a non-empty numeric vector fitdistr(BDF,densfun =“ weibull”,lower = 0)fitdistr(BDF,densfun =“ weibull”,lower = 0)错误:'x'必须是非空数值向量

Sample data I'm using: 2 我正在使用的样本数据:2

200
250
450
320
100
400
200
403
502
203
420
120
342
304
253
423
534
534
243
253
423
123
433
534
234
633
432
342
543
532
123
453
231
532
342
213
243

You are passing a data.frame to fitdistr , but you should be passing the vector itself. 您正在将data.frame传递给fitdistr ,但是您应该传递矢量本身。

Try this: 尝试这个:

d <- read.table(text='200
250
450
320
100
400
200
403
502
203
420
120
342
304
253
423
534
534
243
253
423
123
433
534
234
633
432
342
543
532
123
453
231
532
342
213
243', header=FALSE)

t <- d[1,1]

#Extract the breakdown voltages and toss into dataset, BDV
BDV <- d[-1, 1]

BDF <- (BDV*10000) / t

library(MASS)
fitdistr(BDF, densfun ="weibull", lower = 0)

You could also refer to the relevant column when calling fitdistr , eg: 您还可以在调用fitdistr时参考相关列,例如:

fitdistr(BDF$V1, densfun ="weibull", lower = 0)

#       shape          scale    
#   2.745485e+00   1.997509e+04 
#  (3.716797e-01) (1.283667e+03)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM