1

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.

4
  • for (m in names(a)) should be for (m in names(x)) Commented Aug 14, 2015 at 14:01
  • I have tried that before but when i change that no input dialog appears. Commented Aug 14, 2015 at 14:07
  • 1
    There are a ton of bugs in this code - files<- c("a", "b") should be files<- list(a, b). Enclose you for loops in {}. Think about what new.names looks like, and what new.namesa and b will be Commented Aug 14, 2015 at 14:39
  • Well the files <- list(a,b); did the trick to ask for the column names in both data frames. Commented Aug 14, 2015 at 15:09

1 Answer 1

1

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)
}
Sign up to request clarification or add additional context in comments.

1 Comment

O.k that works just perfectly! I have tried some solutions with the 1:length function and some different parameter (with different outcomes) but i probably would have never come up with such a sophisticated solution. Now i have to take my time to fully understand that. But anyway thank you very much you helped me big time!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.