Deset
Deset

Reputation: 947

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

require (svDialogs)

a<- iris
b<- attitued

> 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.

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). And it assigns the entered new column names to all new created variables (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").

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).

Any Help would be appreciated since I`m really stuck at this point.

Upvotes: 1

Views: 3881

Answers (1)

jeremycg
jeremycg

Reputation: 24945

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)
}

Upvotes: 1

Related Questions