0

I have been trying to find a way of renaming all the columns of each data frame in the workspace in R. They just need to have the same column names. The code below is an example of two data frames (cars and trucks) that will have column names "1:10". However, I have about so many data frames and want to automatically do that.

names(cars) <- c(1:10)
names(trucks) <- c(1:10)

Thanks in advance!

  • will they have the same amount of columns? – Mike Jun 02 '22 at 19:30
  • 1
    You shouldn't have 100 data frames in your workspace. Performing operations on multiple data frames is far easier if you keep them in a list. – Allan Cameron Jun 02 '22 at 19:36
  • @Mike yes, they have the same number of columns – Mustafa Kamal Jun 02 '22 at 19:40
  • Put them in a list: `my_data <- mget(ls(pattern = "df[0-9]+"))` (this particular pattern assumes they are all named `df` followed by numbers). Then set the names, `my_data <- lapply(my_data, setNames, nm = 1:10)`. Read about [why you should use lists of data frames](https://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames/24376207#24376207). But it could boil down to "stuff like this is really easy if they're in a `list`. – Gregor Thomas Jun 02 '22 at 19:41
  • @AllanCameron they are so many data frames that I am preparing code for. The provider told me they are 100 but I guess I can work with them into pieces. – Mustafa Kamal Jun 02 '22 at 19:42
  • @GregorThomas, thank. They have different names. I used df as an example. I will update the manes in the question. thanks! – Mustafa Kamal Jun 02 '22 at 19:45
  • 1
    Okay then, change my first line to `my_data <- Filter(mget(ls()), f = is.data.frame)` which will pull all the data frames into a list. – Gregor Thomas Jun 02 '22 at 19:49

1 Answers1

1

Here is one way to do it. Below I just used mtcars as an example and had one vector in my global env to show you can ignore other objects. First I create a list containing the names of the dfs in the global env. Then I use lapply to set the names to 1 to the length of columns in the data. I name the list the names of the original data.frames and use list2env to export the list to the global env.

edit based on @gregor suggestion

mt1 <- mtcars

mt2 <- mtcars

v1 <- 1

dfslist <-  Filter(mget(ls()), f = is.data.frame)

l1 <- lapply(1:length(dfslist),function(x){
   setNames(dfslist[[x]],1:ncol(dfslist[[x]]))
})

names(l1) <- names(dfslist)

list2env(l1, .GlobalEnv)
Mike
  • 3,797
  • 1
  • 11
  • 30
  • 1
    You can shorten your `dfslist` definition quite a bit using `mget` and the built-in `Filter` function: `dfslist <- Filter(mget(ls()), f = is.data.frame)` – Gregor Thomas Jun 02 '22 at 19:44
  • 1
    Also, in general, don't use `class(x) ==` to check the class of things. Objects may have several classes, and some classes extend others through inheritance. That's why we have the `is.class.name` functions. – Gregor Thomas Jun 02 '22 at 19:46
  • 1
    Though I guess I misunderstood. `dfslist` is just the names of the data frames. I'd recommend skipping that step and going straight to the list of data frames, as in my first comment. – Gregor Thomas Jun 02 '22 at 19:50
  • 1
    With the edits, not `dfslist` is the actual data frames, so your `lapply `can be simplified too, `dfslist <- lapply(dfslist, \(x) setNames(x, 1:ncol(x)))` – Gregor Thomas Jun 02 '22 at 20:29