R - 收到警告:“仅第一个元素用作变量名"; [英] R - Getting warnings: "only the first element is used as variable name"

查看:31
本文介绍了R - 收到警告:“仅第一个元素用作变量名";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 for 循环中使用assign"函数将文件的值分配给一个变量.

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 用于做到这一点,分配"一个值给一个变量(直到语义).我怀疑你想要

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆