0

This is an associated question to rename filename with a data column.

I have files with duplicate names and a data file with additional rows that do not have separate csv files for this analysis. For example: the format of my data file is this:

ID  Cluster  Block  Site  Depth
1   A1       1      1     a
2   A1       1      1     b
3   A1       1      1     c
4   A1       1      1     d
5   A1       2      1     a
6   A1       2      1     b
7   A1       2      1     c
...
600 M1       10     1     a
601 M1       10     1     b
602 M1       10     1     c
603 M1       10     1     d 

The sites and depth repeats in sets of 4. I have a list of csv files for only a and b depths, which mean I only have files with IDs for 1,2,4,5,9,10 etc. These files are labelled slightly differently and have replicates. For example, files in my folder are like this:

1p.csv
1pr.csv
2p.csv
2pr.csv
5p.csv
5pr.csv
6p.csv
6pr.csv

My desired function is to combine data in selected columns for each of the IDs as a file name for corresponding csv files. The csv files are in sets of 2 with a replicate for each file. I am having difficulty adapting the linked question rename filename with a data column because of my limited scripting knowledge and because of the different 'from' and 'to' lengths, which is not allowed for file.rename (). The error message I get is "Error in file.rename(x, y) : 'from' and 'to' are of different lengths."

Thank you so much for your help and suggestions.

Community
  • 1
  • 1
  • I don't quite understand what you're looking for -- are you just renaming files? Also, you have 250 file pairs but are using letters from the alphabet in the new names. What do you want to do after the 26th pair? – josliber Dec 16 '13 at 01:15
  • Also, it sounds like you tried some R code from the link and it didn't work. You should include exactly what you tried and what the error you got -- "because of the different 'from' and 'to' lengths, which is not allowed for file.rename()" is not a lot to go off. – josliber Dec 16 '13 at 01:17
  • @josilber Yes. I want to use the data file to rename the csv files. The files are not paired. This is how the csv files correspond to the data file: for each set of 5 IDs, I have corresponding csv files for only the first two IDs. I.e., ID 1-5 (1p.csv and 2p.csv), ID 6-10 (6p.csv and 7p.csv), ID 11-15 (11p.csv and 12p.csv), etc. Replicate runs on the same ID are numbered the same except there is a letter r for each of these files(ex 1pr.csv). The message I got from file.rename is "Error in file.rename(x, y) : 'from' and 'to' are of different lengths." I hope this helps. – user2770184 Dec 16 '13 at 01:54
  • But you're trying to rename 1p.csv to A1_1_1a.csv and 2p.csv to A1_1_1b.csv. Are you trying to rename 26p.csv to A1_1_1z.csv? What about 27p.csv? Also nobody will be able to debug your code unless you post it. Please edit your post and include the code you tried as well as the error message. – josliber Dec 16 '13 at 02:04
  • I vote to close this question since it is unclear. You should clarify your question by adding the expected output and the exact mapping. – agstudy Dec 16 '13 at 02:28
  • I apologize for the unclear question. I cannot post the output because I don't know how to code it. Essentially, I am trying to match csv file names to its ID in a csv data file. Data file has an ID with other information in the row. Csv file has the same ID except with an additional letter p or pr. I want to use data in the columns next to the ID to rename the files. The problem is that there is no ID in the data file for the replicate csv file. Thank you for your patience. – user2770184 Dec 16 '13 at 03:05

0 Answers0