if statement - error in if() argument is of length zero in R -
this code
`
region<-"arlington"
data<-read.csv("city_markets.csv")
for(i in 1:length(data[[1]])){
if(grep(region,as.character(data[[3]][i]),ignore.case=true)==1){
for(j in 1:length(data)){ write(data[[j]][i],"analyzed.txt",append=true) } }} `
now i'm trying here i'm accessing csv's column(3rd one) , comparing region specified! keep getting error
error in if (grep(region, as.character(data[[3]][i]), ignore.case = true) == :
argument of length zero
to detail bit comment , @adii_ 's :
when use grep, result "position" of elements fulfill condition... "nothing" if there no match (hence error message).
using grepl, you'll true or false, can use in if statement.
as length(grep(...)), result 0 if there no match, corresponding false if statement, or positive integer (1 in case because you're testing 1 element), if there match, corresponding true if statement.
Comments
Post a Comment