我正在尝试创建一个列表,我可以将其复制到data.frame列中的函数的搜索查询中。以下代码的输出格式为:
‘C-484,''F-409,''S-18A,''G-850,''PB-632,'...etc.
但是我需要它来阅读
'C-484','F-409','S-18A','G-850','PB-632', ...etc.
有1,974个变量。如何用逗号切换每个变量的最后一个引号的位置?
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="''")
提前谢谢
答案 0 :(得分:1)
这种方法有效:
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'
因此,在您的具体情况下,它变为:
test1 <- paste0("'", allstations_inactive$STATION, "'", collapse = ",")