2

Forgive my newbie question. I only got started using R since a month ago. This is similar to the question here Strings as variable references in an R formula

Since I can't comment on the answer, I'll just post it here. I have a dataframe called dataxl. In this code, I am using the crosstab() function from http://pcwww.liv.ac.uk/~william/R/crosstab.r. (You can see the explanation of the code here http://rstudio-pubs-static.s3.amazonaws.com/6975_c4943349b6174f448104a5513fed59a9.html) After running the code below, an organized version of CrossTable() function output will be shown:

    ct1 <- crosstab(dataxl, row.vars = "dataxl.tipe_merchant", col.vars = "dataxl.status", type = c("f","t"), style = "wide", addmargins = TRUE)

I am trying to read row.vars from a string variable that stores "dataxl.tipe_merchant", let's call it name. I have tried the following:

name <- paste("dataxl.", "tipe_merchant", sep="")
f <- formula(name)
ct1 <- crosstab(dataxl, row.vars = f, col.vars = "dataxl.status", type = c("f","t"), style = "wide", addmargins = TRUE)

but it keeps saying something around the line

Error in eval(parse(text = x, keep.source = FALSE)[[1L]]) : 
  object 'dataxl.tipe_merchant' not found

....something around object 'dataxl.tipe_merchant' not found. Why is this so? Thank you!

lovalery
  • 4,524
  • 3
  • 14
  • 28
  • 1
    I have no idea what you are trying to achieve using formula. Please provide a reproducible example and the expected result. – Roland Jul 11 '17 at 08:35
  • Use `get(name)` rather than `formula(name)`. – Andrew Gustar Jul 11 '17 at 08:57
  • @Roland it is edited – archipelagoS Jul 11 '17 at 08:58
  • @AndrewGustar I tried it, but it gave Error in get(name) : object 'dataxl.tipe_merchant' not found – archipelagoS Jul 11 '17 at 09:00
  • 1
    @archipelagoS: sorry if I'm misreading, but if your df is named `dataxl` shouldn't there be a `$` after it instead of a period? I mean `dataxl$tipe_merchant`. Or maybe I'm really not understanding this. – Rui Barradas Jul 11 '17 at 10:55
  • @RuiBarradas the data frame column was indeed dataxl.tipe_merchant. There was no problem running the code with just "dataxl.tipe_merchant". The only problem here was how to get row.vars from another variable instead of having to type "dataxl.tipe_merchant" directly. – archipelagoS Jul 12 '17 at 01:28
  • I can't replicate the error. I don't have your data to try it on, so I used the example [at the link you provide](http://rstudio-pubs-static.s3.amazonaws.com/6975_c4943349b6174f448104a5513fed59a9.html). The code they show is `crosstab(Survey, row.vars = "Age", col.vars = "Sex", type = "f")`, which works. I can change it to `col = "age"; crosstab(Survey, row.vars = col, col.vars = "Sex", type = "f")` and it still works. – Gregor Thomas Jan 13 '22 at 00:31
  • I would suggest you delete your delete your `f <- formula(name)` line (as Roland says, not sure what that is trying to do, it seems strange) and try `crosstab(dataxl, row.vars = name, col.vars = "dataxl.status", type = c("f","t"), style = "wide", addmargins = TRUE)`, where `name <- paste("dataxl.", "tipe_merchant", sep="")` as you have it. – Gregor Thomas Jan 13 '22 at 00:32

0 Answers0