r - Error feeding value to read.csv: Must be character string -


i have function defining directory , file(s) loaded , analyzed. values being read in function, when called read.csv, not seem recognize file name. believe issue format, don't know. seems doing should except dt selectdata never created selectdata<-read.csv(z, header=true). z being vector containing file name loaded.

when debugging generated error:

error in read.table(file = file, header = header, sep = sep, quote = quote,  :    'file' must character string or connection 

as error said value being read not character, string or connection, tired set as.character , such no avail. must more simple missing.

when run:

warning message: in mean.default(selectdata$polutant, na.rm = true) :   argument not numeric or logical: returning na 

but, really, selectdata never created (so, mean attempted, has no value)

testfun <- function(directory, polutant, id) {      setwd(directory)              x <- polutant     # not needed checking see if polutant has been read       print(x)          # not needed checking        y <- list.files(directory, full.names=true)      print(y[id])      # not needed checking     z <- y[id]      if (length(id == 1)) {         selectdata <- read.csv(z, header = true)     }     mean(selectdata$polutant, na.rm=true)  }   

there several errors in code, not related error message. still, let’s take them in turn, result may make obvious error is:

x <- polutant     # not needed checking see if polutant has been read   print(x)          # not needed checking   

not error, no need assign polutant variable. print directly.

y <- list.files(directory, full.names=true) 

you specify directory directory, chdir’d directory. you’re looking in directory/directory. consequently, you’ll not find files.

since chdir has side-effect observable outside function, it’s not idea use anyway. remove it.

print(y[id])      # not needed checking 

what print? null – causes error message.

if (length(id == 1)) { 

first compare id 1, then check whether length of unequal 0 (if (some_number) sloppy shortcut if (some_number != 0)). wanted write if (length(id) == 1).

mean(selectdata$polutant, na.rm=true)  

there 2 errors here. firstly, cannot access selectdata outside scope it’s been defined in (i.e. inside if). secondly, try access column in data frame name polutant. want access column name that’s stored in variable polutant. cannot use $ syntax that, need use either selectdata[[polutant]] or selectdata[, polutant].

which leaves with:

testfun <- function(directory, polutant, id) {     filenames <- list.files(directory, full.names = true)      if (length(id) == 1) {         filename <- filenames[id]         selectdata <- read.csv(filename, header = true)         mean(selectdata[, polutant], na.rm = true)     } } 

(i’ve taken liberty of unifying variable naming , formatting convention, , using more descriptive variable names.)

also, aware list.files returns filenames in unspecified, , potentially changing order, cannot meaningfully use fixed id load given file.


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -