This is my first post, I am quite new to R (one month) so I might have some really stupid mistake here.
I am trying to write a function to:
My sample string looks like this:
tweedehands?merkIds=9fdc7e2d-b40c-4ee6-896d-9f0453cb39c6&modelIds.9fdc7e2d-b40c-4ee6-896d-9f0453cb39c6=d56286d3-cecc-4183-9b6d-9c7e241de6c1&minimumprijs=5000&maximumprijs=6000&minimumbouwjaar=&maximumbouwjaar=&minimumkilometerstand=&maximumkilometerstand=&postcode=&afstand=
For example, I want to extract the value after minimumprijs=
and before &
, the function should return 5000
install.packages("cwhmisc")
library("cwhmisc")
getFilter <- function(x, STAchar = NA, ENDchar = "&"){
x<- as.character(x)
# pre-allocate a vector to store the extracted strings
snippet = rep(0, length(x))
for (i in 1:length(x))
{#extract the initial position
staPostion = (cpos(x[i], STAchar, 1) + nchar(STAchar))
# extract the final position
endPostion = (cpos(x[i], ENDchar, staPostion) - length(ENDchar))
# extract the substring between the initial and final positions, inclusively
snippet[i] = substr(x[i], staPostion, endPostion)
}
return(snippet)
}
However, this function is not working for me and I don't really understand what is the problem.
The error it returns is as follow:
Error in if (start + lsub1 > lstr) return(NA) else { : missing value where TRUE/FALSE needed Called from: cpos(x[i], ENDchar, staPostion) Browse[1]>
Now I can only think about to add some Error Handling, NA Handling lines inside of the function. But maybe it is something else.
Thanks for your help!!!
感谢您的所有帮助,但我刚刚发现使用 str_locate 和 substr 有一种更有效的方法来实现相同的功能
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.