R-收到警告:“仅第一个元素用作变量名". [英] R - Getting warnings: "only the first element is used as variable name"
问题描述
我正在尝试在for循环中使用赋值"功能,将文件的值分配给一个变量.
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
如果我运行> corr("specdata",129),我可以看到正确的答案,它可以打印所有正确的值,但是例如,如果我将这些值分配给任何变量,则表示该变量为"NULL ".一个例子:
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
它将为我提供符合此条件的所有值,但似乎无法将这些值传递给"cr".其他示例:
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")
}
}
我是R语言的入门者,因此,如果有任何方法可以解决此问题或从文件夹中读取每个文件并单独进行操作,
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
fileValues <- read.csv(fileList[i])
这篇关于R-收到警告:“仅第一个元素用作变量名".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!