0

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
  }
steveb
  • 5,382
  • 2
  • 27
  • 36
I_AM_JARROD
  • 685
  • 2
  • 7
  • 20
  • 1
    Please see [How to make a great R reproducible example?](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Part of the question should be a small set of input data (use `dput(your_data_frame)` to reproduce the issue. – steveb Jun 15 '18 at 22:16
  • 3
    you could try adding `str(z2)` right before the line with `z2$a` to see what `z2` looks like right before the error happens - or, if the output would be voluminous, use `options(error=recover)` to drop into a debugger when the error happens – Ben Bolker Jun 15 '18 at 22:24
  • 1
    probably `z2` is a vector only.. – SeGa Jun 15 '18 at 22:41
  • @BenBolker Thank you both. I'm back up and rolling. I added as.data.frame to convert to a dataframe and all is well now. – I_AM_JARROD Jun 15 '18 at 23:38

1 Answers1

1

z2 was not being created as a data frame. adding as.data.frame() took care of everything.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
I_AM_JARROD
  • 685
  • 2
  • 7
  • 20