0
votes

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
2
the file extension ".0" is what you get with OPUS spectra files. - Cathyt10
Yes. I have read it in using as.data.frame. - Cathyt10
Can you please show us the structure of that dataframe; copy the output of str(yourDataframe) in your question, i.e. edit your question: stackoverflow.com/posts/52499275/edit - jogo
'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
Yes, thank you, strings as factors was the problem, it worked now. You're a star :-) - Cathyt10

2 Answers

4
votes

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]] )
0
votes

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