简体   繁体   English

R:循环通过多个变量

[英]R: loop though multiple variables

I have got Multiple data.frames (from the datasets package) loaded and I Use the svDialogs package for some simple data input 我已经加载了多个data.frames(来自数据集包),并且使用svDialogs包进行一些简单的数据输入

require (svDialogs) 需要(svDialogs)

a<- iris 虹膜
b<- attitued b <-姿势

> a
     Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
1            5.1         3.5          1.4         0.2     setosa
2            4.9         3.0          1.4         0.2     setosa
3            4.7         3.2          1.3         0.2     setosa
4            4.6         3.1          1.5         0.2     setosa
5            5.0         3.6          1.4         0.2     setosa
and so on


> b
   rating complaints privileges learning raises critical advance
1      43         51         30       39     61       92      45
2      63         64         51       54     63       73      47
3      71         70         68       69     76       86      48
4      61         63         45       47     54       84      35
5      81         78         56       66     71       83      47
and so on

The loaded data frames are not always the same and derived from previous steps so their naming and number can change. 加载的数据帧并不总是相同,而是从先前的步骤派生而来的,因此它们的名称和编号可以更改。

What I want to do now is to loop though those data frames and ask how the column names should be renamed and each of the data frames should get a new variable new.names[FILENAME] in this case new.names.a and new.names.b so that in a later step those could be assigned as the new column names for the specific data.frames. 我现在想做的是循环遍历那些数据框,并询问应如何重命名列名称,并且在这种情况下,每个数据框都应获得一个新变量new.names [FILENAME] new.names.a和new。 names.b,以便在后续步骤中可以将其分配为特定data.frames的新列名。

I think the first thing I need is to store the previously loaded dataframes in a variable 我认为我需要做的第一件事是将先前加载的数据帧存储在变量中

files<- c("a", "b")

and apply something like this: 并应用如下内容:

new.names <- c()
for (x in files)  
  for (m in names(a))
    new.names <- c(new.names,dlgInput(sprintf('Enter new column name or press ok: "%s"', m), default=m, Sys.info()["n"])$res)
   for(i in files)
     assign(paste("new.names", i,sep=""), new.names)

Well it works for one data frame but not with multiple (it just repeats the column names of the same data frame as many times as there are values in the "files" variable). 很好,它适用于一个数据帧,但不适用于多个数据帧(它只是将同一数据帧的列名重复“ files”变量中的值的次数多次)。 And it assigns the entered new column names to all new created variables (new.namesa, new.namesb). 并将输入的新列名称分配给所有新创建的变量(new.namesa,new.namesb)。 Actually, it should skip to the next variable (new.nameb) as soon as it starts to iterate the names of the second file ("b"). 实际上,它应该在开始迭代第二个文件的名称(“ b”)时立即跳至下一个变量(new.nameb)。

The output if i don`t change the suggested original column names is: 如果我不更改建议的原始列名称,则输出为:

> new.namesa
 [1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width"  "Species"      "Sepal.Length"
 [7] "Sepal.Width"  "Petal.Length" "Petal.Width"  "Species"     
> new.namesb
 [1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width"  "Species"      "Sepal.Length"
 [7] "Sepal.Width"  "Petal.Length" "Petal.Width"  "Species" 

So is there a possibility to solve this? 那么有可能解决这个问题吗? I`m not to keen on using loops - i have read multiple times that those should be avoided in R.- if there is another solution (in the best of cases but not necessarily without the use of additional packages). 我不热衷于使用循环-我已经读过很多遍了,R中应该避免使用循环-如果有另一种解决方案(在最佳情况下,但不一定使用其他程序包)。

Any Help would be appreciated since I`m really stuck at this point. 任何帮助将不胜感激,因为我确实停留在这一点上。

Your code is a little tangled, here is an easier way to do what you want: 您的代码有些混乱,这是一种执行所需操作的简便方法:

require (svDialogs)
a <- iris
b <- attitude

dfs <- c("a", "b")

for(df in dfs) {
  df.tmp <- get(df)
  for(i in 1:length(names(df.tmp))){
    names(df.tmp)[i] <- dlgInput(sprintf('Enter new column name or press ok: "%s"', names(df.tmp)[i]), default=names(df.tmp)[i], Sys.info()["n"])$res
  }
  assign(df, df.tmp)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM