连接变量matlabfile在R中的字符串名称 [英] Concatenating string names of variables matlabfile in R

查看:280
本文介绍了连接变量matlabfile在R中的字符串名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有matlab文件在我的变量里面的每个名称的整数(除了第一个)。我想循环连接整数的名称。
有我的代码:

I have matlab files with an integer in each name of my variables inside (except the first one). I want to loop to concatenate the name of the integers. There is my code:

library('R.matlab')
mat <- readMat('SeriesContPJM.mat')
#str(mat)
#typeof(mat)
                                        #mat[[1]]
write.csv(mat$vol.PJM$data[[4]][[1]], "PJM.csv")
i = 2
while (i < 7)
{
    write.csv(get(paste("mat$vol.PJM", as.character(i), "$data[[4]][[1]]", sep = "")), paste(paste("PJM", as.character(i), sep="_"), "csv", sep ="."))
    i = i + 1
}

我有 write.csv(mat $ vol.PJM $ data [[4]] [[1]],PJM.csv) 这给了我良好的输出。我想在循环中的其他变量名相同,但我得到以下ouput:

I have write.csv(mat$vol.PJM$data[[4]][[1]], "PJM.csv") which gives me the good ouput. I would like the same for the other variable names in the loop but I get the following ouput:

+ Error in get(paste("mat$vol.PJM", as.character(i), "$data[[4]][[1]]",  (from importpjm.R#10) : 
  objet 'mat$vol.PJM2$data[[4]][[1]]' introuvable



<

"introuvable" means "not found" in French.

推荐答案

这里你需要使用 get 并且您需要使用 eval(parse())

Here you're mixing where you need to use get and where you need to use eval(parse()).

c $ c> get 使用字符串变量游戏,例如 get(mtcars),但 code>是需要评估的函数。

You can use get with a string variable game, e.g., get("mtcars"), but [ is a function that needs to be evaluated.


  • get(mtcars [ 2,2])将无法工作,因为您没有名为的变量mtcars [2,2]一个名为mtcars的变量和一个名为[ ,2

  • get("mtcars[2, 2]") won't work because you don't have a variable named "mtcars[2, 2]", you have a variable named "mtcars" and a function named "[" that can take arguments 2, 2.

eval(parse(text =mtcars [2,2] 将工作,因为它不只是寻找一个变量,它实际上评估文本字符串,如果你输入到命令行。

eval(parse(text = "mtcars[2, 2]")) will work because it doesn't just look for a variable, it actually evaluates the text string as if you typed it into the command line.



    所以,你可以重写你的循环,用 eval替换 get(...) (parse(text = ...)),它可能会工作,假设你粘贴 d的字符串有正确的语法。但这可能很难读取和调试。在6个月内,如果你回头看看这段代码,并且需要理解它或修改它,它将会让人困惑。

So, you could rewrite your loop, replacing the get(...) with eval(parse(text = ...)) and it would probably work, assuming the strings you've pasted together have the right syntax. But this can be difficult to read and debug. In 6 months, if you look back at this code and need to understand it or modify it, it will be confusing.

另一种方法是使用 [[用字符串提取子列表。而不是混淆 eval(parse())我会这样做:

Another way to do it would be to use [[ with strings to extract sublists. Rather than mess with eval(parse()) I would do this:

vols = paste0("vol.PJM", 2:7)
for (vol in vols) {
    write.csv(mat[[vol]][["data"]][[4]][[1]],
              paste0(vol, ".csv"))
}

我认为它更易读,并且很容易预先调试 vols 向量,以确保所有的名称是正确的。类似地,如果你想遍历所有的元素,你可以初始化 vols names(mat)或使用某种正则表达式标准从 names(mat)中提取适当的子列表。

I think it's more readable, and it's easy to debug the vols vector beforehand to make sure all your names are correct. Similarly, if you want to loop over all elements, you could initialize the vols as something like names(mat), or use some sort of regex criteria to extract the appropriate sublists from names(mat).

这篇关于连接变量matlabfile在R中的字符串名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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