我的代碼的目的是連接向量 v 中的值。為此,我創建了一個帶有兩個引數向量 SID 的 concat 函式。但出于我不明白的原因,
#A character vector to which other strings will be appended
v <- c("R_2wmKOSbPWHl4VtT2","R_2TtslLEVNeHs2r73","R_ZF79IJ60LaxxsuR4","R_3JJDUkrZ07eIwnh5","R_3JrWuv9fsLK6qNx6")
concat <- function(vector,SID){
decrement_append <- "&decrementQuotas=true"
SID_append <- "?surveyId="
for(i in 1:length(vector)){
out[i] <- paste0(v[i],SID_append,SID,decrement_append)
}
out[i]
}
和:
concat(vector = v,
SID = "SV_55tYjKDRKYTRNIh")
當我運行這個時,我得到:
Error in concat(vector = v, SID = "SV_55tYjKDRKYTRNIh") :
object 'out' not found
我已經嘗試了其他幾種方法,例如:
concat <- function(vector,SID){
decrement_append <- "&decrementQuotas=true"
SID_append <- "?surveyId="
new_vector <- for(i in 1:length(vector)){
out[i] <- paste0(v[i],SID_append,SID,decrement_append)
}
new_vector
}
但我遇到了同樣的錯誤。
uj5u.com熱心網友回復:
該out不是在初始化函式
concat <- function(vector,SID){
out <- character(length(vector))
decrement_append <- "&decrementQuotas=true"
SID_append <- "?surveyId="
for(i in 1:length(vector)){
out[i] <- paste0(v[i],SID_append,SID,decrement_append)
}
out
}
-測驗
> concat(v, "SV_55tYjKDRKYTRNIh")
[1] "R_2wmKOSbPWHl4VtT2?surveyId=SV_55tYjKDRKYTRNIh&decrementQuotas=true" "R_2TtslLEVNeHs2r73?surveyId=SV_55tYjKDRKYTRNIh&decrementQuotas=true"
[3] "R_ZF79IJ60LaxxsuR4?surveyId=SV_55tYjKDRKYTRNIh&decrementQuotas=true" "R_3JJDUkrZ07eIwnh5?surveyId=SV_55tYjKDRKYTRNIh&decrementQuotas=true"
[5] "R_3JrWuv9fsLK6qNx6?surveyId=SV_55tYjKDRKYTRNIh&decrementQuotas=true"
paste/paste0被矢量化。所以,回圈并不是真正需要的
concat2 <- function(vector,SID){
decrement_append <- "&decrementQuotas=true"
SID_append <- "?surveyId="
paste0(v, SID_append,SID,decrement_append)
}
-測驗
> concat2(v, "SV_55tYjKDRKYTRNIh")
[1] "R_2wmKOSbPWHl4VtT2?surveyId=SV_55tYjKDRKYTRNIh&decrementQuotas=true" "R_2TtslLEVNeHs2r73?surveyId=SV_55tYjKDRKYTRNIh&decrementQuotas=true"
[3] "R_ZF79IJ60LaxxsuR4?surveyId=SV_55tYjKDRKYTRNIh&decrementQuotas=true" "R_3JJDUkrZ07eIwnh5?surveyId=SV_55tYjKDRKYTRNIh&decrementQuotas=true"
[5] "R_3JrWuv9fsLK6qNx6?surveyId=SV_55tYjKDRKYTRNIh&decrementQuotas=true"
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/403220.html
標籤:
下一篇:httrGET函式超時
