Reputation: 685
I am running a function with the code below inside the function.
Should I be declaring something as a dataframe where I am not?
When it runs I get
"Error: $ operator is invalid for atomic vectors"
If I redefine z2$a
as z2[,1]
in the creation of the ma2
dataframe I get.
Error in z2[, 1] : incorrect number of dimensions
#---identifieds the loaction of the business address header and mailing address header
ba <- grep("BUSINESS ADDRESS:",z$a)
ma <- grep("MAIL ADDRESS:", z$a)
#-- create the business address dataframe from the main dataframe "z"
#--- if no "business address" header is found then set z2 dataframe to NA
#--- if yes on "business address" header set the z2 fram to the header location and all
#--- rows below
if(length(ba) > 0) {
z2 <- z[ba:nrow(z),,drop = FALSE]
} else {
z2 <- NA
}
#--- trim the business address df z2 if the mailing address is in the dataframe.
#--- if mailing address header "MAIL ADDRESS:" is found then the if statement
#--- will chop off mailing address and everything below it.
#--- if mailing address header is not found the z2 dataframe will remain as is
ma2 <- grep("MAIL ADDRESS:",z2$a)
if(length(ma2) > 0) {
z2 <- z2[1:((ma2)-1),,drop = FALSE]
} else {
z2
}
Upvotes: 0
Views: 4583
Reputation: 685
z2
was not being created as a data frame.
adding as.data.frame()
took care of everything.
Upvotes: 1