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")
 }
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63
B.Wel
  • 86
  • 10
  • 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](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to make it easier to help you. – MrFlick Aug 22 '16 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. – B.Wel Aug 22 '16 at 14:01

3 Answers3

1

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

paste(filenm,"_unc",".tif",sep="")
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
  • 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. – B.Wel Aug 22 '16 at 14:12
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")
}
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63
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")
}
Community
  • 1
  • 1
ChristianL
  • 140
  • 7
  • That is exactly what I was after! Thank you so much for the help. – B.Wel Aug 22 '16 at 14:42
  • 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. – Robert Hijmans Aug 23 '16 at 09:39