简体   繁体   中英

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. When I try to use fitdistr() I get an error stating " '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. 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

Sample data I'm using: 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.

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(BDF$V1, densfun ="weibull", lower = 0)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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