I'm trying to use "assign" function, inside a for loop, to assign the value of a file to one variable.
When I call the function, it brings the correct answer but in the end it gives me the following warning messages:
In assign(fileList, read.csv(fileList[i])) :
only the first element is used as variable name
If I run > corr("specdata", 129) I can see the correct answer, it can print all the right values, but If I assign the values to any variable for example, it says that this variable is "NULL". An example:
cr <- corr("specdata", 150)
head(cr)
NULL
It will give me all the values that fit in this criteria but it seems that it can't pass the values to "cr". Other examples:
> class(cr)
[1] "NULL"
> cr
NULL
The code that I'm currently using, if is helpful:
corr <- function(directory, threshold){
if (directory == "specdata"){
setwd("C:/Users/User/Downloads/specdata")
fileList <- list.files(pattern="*.csv")
for (i in 1:length(fileList)){
fileValues <- assign(fileList, read.csv(fileList[i]))
okValues <- na.omit(fileValues)
completeCases <- sum(complete.cases(okValues))
if (completeCases > threshold) {
sulfate <- okValues["sulfate"]
nitrate <- okValues["nitrate"]
correlation <- cor(sulfate, nitrate, use="complete.obs", method=c("pearson", "kendall", "spearman"))
#print(correlation)
}
else if (completeCases <= threshold){
#print("0")
}
i = i+1
}
}
else {
print("There's no such directory")
}
}
I'm a begginer on R language, so, if there's any way to fix this issue or to read every single file from a folder and manipulate separately, I'd be glad.
assign
is used to do just that, "assign" a value to a variable (up to semantics). I suspect you want