0
votes

So I'm new to R and I am trying to make the columns of this dataset more readable:

column names

with the rename() function from dplyr:

  x <- names(data)
  cols <- gsub('\\.', ' ', x)
  
  for (col in ncol(data)) {
    data <- data %>% rename(cols[col] = names(data)[col])
    col
  }

but I get an error, which I can't fix with my knowledge or google:

Error in source("~/Desktop/r/assignment1/best.R") : 
  ~/Desktop/r/assignment1/best.R:9:45: unexpected '='
8:   for (col in ncol(data)) {
9:     outcome <- data %>% rename(cols[col] =

I would be very grateful if someone could tell me where to look for the solution to this.

Thanks a lot!

2
names(data) <- gsub('\\.', ' ', names(data)) - Ronak Shah
It is generally not recommended to have spaces in column names. However, you can simply do names(data) <- gsub(...) No loop needed or anything - Sotos
The issue is that while in base R you can do selective reassignment of vectors with vec[ind] <- 1, that does not work in dplyr functions, so rename(cols[col] = ...) or mutate(cols[col] = ...) or similar will not work. There are workarounds (perhaps if_else in some cases), but I believe that the gsub workaround here is more succinct. - r2evans

2 Answers

0
votes

An option is to use dplyr::rename_all. You need to provide a function for renaming. An example would be as follows:

mtcars %>% rename_all(~paste0(.,"_renamed"))

This will add the _renamed postfix to each and every column name. In your case, you can use:

df %>% rename_all(~gsub("\\.", " ", .))

Keep in mind, though, that spaces in column names is BAD idea.

0
votes

A couple of things about the R way of thinking that you need to get more familiar with:

  • Learn the difference between names and colnames
  • Avoid spaces in column names whenever you can, never create them yourself
  • A for loop should never be your first option, R works on blocks of stuff all at once.
    I actually feel that you shouldn't get help on this question because putting spaces into column names in R is wrong.
    But, if you want to manipulate column names in a dataframe, you can do it directly, but I'm going to change the dots to an underscore, because that's ok.
    colnames(data) <- gsub("\\\\.", "_", colnames(data))