0

I am trying using the re-name function and have selected the correct working directory and used list.files to get the old names, but not sure what to do now. I have a columns in a CSV file which precisely match the old name and the new name:
PS. the file extension ".0" is what comes with OPUS spectra files.

old-name    new-name
roth_666_1.0    N149_1.0
roth_666_2.0    N124_1.0
roth_666_3.0    N36_1.0
roth_666_4.0    N59_1.0
roth_666_5.0    N140_1.0
roth_666_6.0    N95_1.0
roth_666_7.0    N74_1.0
roth_666_8.0    N81_1.0
roth_666_9.0    N157_1.0
roth_666_10.0   N27_1.0
roth_666_11.0   N66_1.0
roth_666_12.0   N131_1.0
roth_666_13.0   N118_1.0
roth_666_14.0   N15_1.0
roth_666_15.0   N22_1.0
roth_666_16.0   N53_1.0
Cathyt10
  • 71
  • 8
  • the file extension ".0" is what you get with OPUS spectra files. – Cathyt10 Sep 25 '18 at 13:25
  • Yes. I have read it in using as.data.frame. – Cathyt10 Sep 25 '18 at 13:32
  • Can you please show us the structure of that dataframe; copy the output of `str(yourDataframe)` in your question, i.e. edit your question: https://stackoverflow.com/posts/52499275/edit – jogo Sep 25 '18 at 13:38
  • 1
    'data.frame': 756 obs. of 2 variables: $ old.name: Factor w/ 756 levels "roth_666_1.0",..: 1 25 47 69 91 113 135 157 179 3 ... $ new.name: Factor w/ 756 levels "N1_1.0","N1_2.1",..: 107 55 175 225 89 305 259 275 125 155 ... – Cathyt10 Sep 25 '18 at 13:58
  • jogo, thanks, but I get this error: Error in (function (from, to) : invalid 'from' argument. – Cathyt10 Sep 25 '18 at 14:04
  • Please use `stringsAsFactors=FALSE` for reading your CSV-file! Check your dataframe after reading! `str(...)` – jogo Sep 25 '18 at 14:06
  • 1
    Yes, thank you, strings as factors was the problem, it worked now. You're a star :-) – Cathyt10 Sep 25 '18 at 14:12
  • Please read https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – jogo Sep 25 '18 at 14:14

2 Answers2

4

So assuming you have a data frame df with columns "old-name" and "new-name" as above, and assuming that the current working directory is where the files are:

# will not work due to hyphens in column names 
# file.rename(from = df$old-name, to = df$new-name)

# better - specify the vector of values held in each column of the dataframe
file.rename(from = df[[1]], to = df[[2]] )
stenevang
  • 116
  • 7
  • What error is that? Actually, @stenevang ... hyphen is a special character and so names should be escaped with backticks, otherwise a subtraction expression is assumed here. – Parfait Sep 25 '18 at 14:24
  • Thanks, @Parfait - I overlooked the use of hyphens in the column names of the example. – stenevang Sep 25 '18 at 14:30
  • @Cathy10 - the dashes or hyphens are big no-nos for use in column names or any object names in R. – stenevang Sep 25 '18 at 14:30
  • @Cathy10 you may see a problem because your dataframe uses stringsAsFactors = TRUE. – stenevang Sep 25 '18 at 14:36
  • Thanks stenevang the strings as factors were the problem. And thanks for the heads up about the hyphens. – Cathyt10 Sep 25 '18 at 16:54
0

@stenevang is correct

o <- c("roth_666_1.0","roth_666_2.0"  ,"roth_666_3.0" )
n <- c("N149_1.0","N150_1.0","N529_1.0")

df <- data.frame(o,n,stringsAsFactors=FALSE)

file.rename(df$o,df$n)
Carlos Santillan
  • 1,077
  • 7
  • 8