有些时候我使用save函数将一些列表变量保存为文件
> str(ttr.varSD)
List of 4
$ classifierLimits: Named num [1:5] 2 13 5 24 16
..- attr(*, "names")= chr [1:5] "sdClose-VS" "sdDiff-VS"...
$ trainClassLabels: num [1:497] 4 2 3 4 2 3 2 4 1 4 ...
$ testClassLabels : num [1:497] 4 2 2 4 4 4 4 4 4 4 ...
>
> save(ttr.varSD, file='ttr.varSD.RDS')
Run Code Online (Sandbox Code Playgroud)
现在我想使用load(file='ttr.varSD.RDS')
函数检索它们但它返回此错误.
>load(file='ttr.varSD.RDS')
Error: bad restore file magic number (file may be corrupted) -- no data loaded
In addition: Warning message: …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用带有fnMap参数的DEoptim,如文档"在创建每个填充后将运行的可选函数"中所述,因此我创建了这个简单的测试用例.
fnm <- function(x) round(x, 2)
fn <- function (x) x ^ 2
upper <- 100
lower <- -100
DEoptim(fn=fn, lower=lower, upper=upper, fnMap=fnm)
Run Code Online (Sandbox Code Playgroud)
问题是当我使用fnMap参数时,它返回一个错误{ 映射函数没有为任何类型的map函数返回一个带有dim NP x length(upper) } 的对象.
在R语言中,有一些函数,如名称和dimnames,您可以为它们分配值作为示例:
x <- list('foo'=2, boo=3)
names(x) # This returns ("foo", "boo") vector
names(x) <- c("moo", "doo") # Changes existing item names to ("moo", "doo")
Run Code Online (Sandbox Code Playgroud)
我的问题是如何创建这样的功能,显然它们可以作为集合并同时获取功能.
我想为R中的学生找到成绩最高的科目。我编写了此函数,该函数很好用,但它看起来比R更像Java代码,因为它使用循环遍历向量而不是为此目的使用特殊函数。 R.请您提出一个替代解决方案
grades <- c(62, 100, 45, 40, 46, 55, 56, 70)
largestFail <- function(grades){
location <- -1
faildFound <- FALSE
for(i in 1:length(grades)){
if(grades[i] < 50){
if(!faildFound) location <- i
if(grades[i] > grades[location]) location <- i
faildFound <- TRUE
}
}
return (location)
}
print (grades) # 5
Run Code Online (Sandbox Code Playgroud)