1

I am trying to create a list that I can copy into a search query for a function from a data.frame column. My output from the below code is in the format of :

‘C-484,''F-409,''S-18A,''G-850,''PB-632,'...etc.

But I need it to read

'C-484','F-409','S-18A','G-850','PB-632', ...etc.

There are 1,974 variables. How can I switch the placement of the last quote around each variable with the comma?

inactivestations <- read.csv("INACTIVE_WELLS.csv",header=TRUE)

#subset and make data.frame for only STATION (station names)
allstations_inactive <- inactivestations['STATION'] 

#not separated in a way that can be copied into a query
list(allstations_inactive$STATION) 

#separated by commas and has quotes around each variable but commas inside quotes
test<-paste0(allstations_inactive$STATION, collapse="''",sep=",") 

##separated by commas and has quotes around each variable but commas inside quotes
test1<-paste0(allstations_inactive$STATION, sep=",",collapse="''") 

Thank you in advance

user3
  • 37
  • 1
  • 6
  • It would be easier to help you if you provided a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with a sample input dataset. I don't think we need all 2000 variables, just a few should get the point across. – MrFlick Apr 07 '17 at 21:40
  • I am new to R and this is my first day on stack overflow. Is your edited version of my question considered a reproducible example? If it is, it seems really similar to what I originally posted, and I'm unclear about the meaning of a reproducible example. I will search more about this unless you can please elaborate for me, @MrFlick. – user3 Apr 07 '17 at 21:53
  • No. I didn't make it reproducible, I just cleaned up the formatting so it's easier to read. The link I provided has examples of including data in questions. Notice how at least one of the answers provided their own data in a format that you can copy/paste it to R to run. That is ideal. – MrFlick Apr 07 '17 at 21:58

1 Answers1

1

This approach works:

input <- c("C-484", "F-409", "S-18A", "G-850", "PB-632")
output <- paste0("'", input, "'", collapse = ",")
# cat(output)
# 'C-484','F-409','S-18A','G-850','PB-632'

So in your specific case it becomes:

test1 <- paste0("'", allstations_inactive$STATION, "'", collapse = ",")