Hi, May be this helps: number_of_stations <- 3 label <- c("s1","s2","s3","s4") label1 <- combn(label[1:number_of_stations],2) label.i <- character(0)calc_distances <- function(z,a){ for(i in 1:z){lab.i <- paste(a[1,i],a[2,i],sep="-") label.i <- c(label.i,lab.i)} data.frame(label=label.i,stringsAsFactors=FALSE) } calc_distances(number_of_stations,label1) # label #1 s1-s2 #2 s1-s3 #3 s2-s3 number_of_stations <- 5 label <- c("s1","s2","s3","s4" ,"s5") label1 <- combn(label[1:number_of_stations],2 ) calc_distances(number_of_stations,label1) #label #1 s1-s2 #2 s1-s3 #3 s1-s4 #4 s1-s5 #5 s2-s3? #or data.frame(label=apply(label1[,1:number_of_stations],2,paste,collapse="-"),stringsAsFactors=FALSE) A.K. Hi, I have read several pages and threads on this error message, but I haven't found one that solved my problem, so any help would be appreciated. As part of a much larger script, I am trying to generate a label column that has all possible combinations from a vector, such that for the vector c("s1","s2","s3") I would get a column "s1-s2" "s1-s3" "s2-s3" I need the script to be imbedded within a for loop that is carrying out other functions, so here is the snip it of code that is relevant to this column number_of_stations <- 3 label <- c("s1","s2","s3","s4") label <- combn(label[1:number_of_stations],2) #this script is designed to be run using various numbers of stations which will not always be the same as the number of labels lable.i <- as.data.frame(NULL) calc_distances <- function(z,a){ for(i in 1:z){lab.i <- gsub(" ","",paste(a[1,i],"-",a[2,i])) label.i <- as.data.frame(rbind(label.i,lab.i))} print(lable.i)} calc_distances(number_of_stations,label) When I run that script, I get the following X.s1.s2. 1 s1-s2 2 3 Warning messages: 1: In `[<-.factor`(`*tmp*`, ri, value = "s1-s3") : invalid factor level, NA generated 2: In `[<-.factor`(`*tmp*`, ri, value = "s2-s3") : invalid factor level, NA generated So, my question is, why does it run on the first set, but have issues with the other sets? Also, If I extract this code from the for loop and run the gsub line separately for each combination, it works fine (e.g. gsub(" ","",paste(label[1,2],"-",label[2,2])) doesn't give me that error message). How do I solve this error? Thanks for the help