0

I have a loop in which I want to create a character vector of output file names by combining elements in rasnames with "_unc.tif"

rasnames = list("Wheat","Sbarley","Potato","OSR","fMaize") 

I tried

for (i in 1:length(rasnames)){
  filenm <- rasnames[i]
  filenm <- c(filenm,"_unc",".tif")
 }
2
  • 1
    Perhaps you want paste0(filenm,"_unc",".tif") rather than using c(), but the error message seems to thing rasnames might be a list. You really should provide a reproducible example to make it easier to help you. Commented Aug 22, 2016 at 13:52
  • I didn't want to put the entire function up as it would make for a very long question! I tried your suggestion, paste0 helped as I now get a raster file but I get a copy with each item in the list. Commented Aug 22, 2016 at 14:01

3 Answers 3

1

You should use paste(), instead of c(). c() creates a list of strings, rather than one concatenated string:

paste(filenm,"_unc",".tif",sep="")
Sign up to request clarification or add additional context in comments.

1 Comment

As above I tried that and I get a copy of the raster for each name in the list. I'll add a model raster dataset to the question.
1

Do not make a list (or if you cannot help that, use unlist)

rasnames = c("Wheat","Sbarley","Potato","OSR","fMaize") 

Make a vector of output names:

outnames = paste0(rasnames, "_unc.tif")

for (i in 1:length(rasnames)){
  filenm <- outnames[i]
}

Or:

for (i in 1:length(rasnames)){
  filenm <- paste0(rasnames[i], "_unc.tif")
}

2 Comments

Thanks for your feedback. I realized the simplest fix was just to use deparse and remove the extra step of defining vectors etc as per ChristianL's answer.
using deparse is very bad practice.
0

If I understand your question correct you want to use the name of an object as a part of a file name. You can use deparse(substitute(obj)):

    ftest <- function(df) {
paste0(deparse(substitute(df)), ".tif")
}

ftest(iris)

# Output:
# [1] "iris.tif"

See: How to convert variable (object) name into String

If you want to use a list of strings as file names:

ftest2 <- function(lst) {
  for (i in 1:length(lst)) {
    filename <- lst[[i]]
    filename <- paste0(filename, ".tif")
    print(filename)
  }
}
rasnames = list("Wheat","Sbarley","Potato","OSR","fMaize")
ftest2(rasnames)

# Output:
# [1] "Wheat.tif"
# [1] "Sbarley.tif"
# [1] "Potato.tif"
# [1] "OSR.tif"
# [1] "fMaize.tif"

Here is a alternative version without using deparse(substitute()). This code read files from a directory and saves them with the prefix "df_" in a directory called "mynewfiles".

# create some text files in your working directory using the the "iris" data
write.table(iris, file = "test1.txt", sep = "\t")
write.table(iris, file = "test2.txt", sep = "\t")

# get the file names
myfiles <-  dir(pattern = "txt")

# create a directory called "mynewfiles" in your working directory 
# if it doesn't exists

if (!file.exists("mynewfiles")) {
  dir.create("mynewfiles")
}

for (i in 1:length(myfiles)) { 
  dftmp <- read.table(myfiles[i], header = TRUE, sep = "\t")
  # insert code to do something with the data frame...
  filename <- paste0("df_", myfiles[i])
  print(filename)
  write.table(dftmp, file = file.path("mynewfiles", filename), sep = "\t")
}

2 Comments

That is exactly what I was after! Thank you so much for the help.
There really should not be a need for that using deparse (how did the objects get names?). It is bad advise to give to someone who is struggling with the basics of writing a loop and pasting strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.