Hi, I'm trying to create a simple function that takes a dataframe as its only argument. I've been using gmodels::CrossTable, but it requires a lot of arguments, e.g.: #this runs fine CrossTable(data$col1, data$col2, prop.chisq = FALSE, prop.c = FALSE, prop.t = FALSE, format = "SPSS") Moreover, I wanted to make it compatible with piping, so I decided to create the following function: ctab <- function(data) { CrossTable(data[,1], data[,2], prop.chisq = FALSE, prop.c = FALSE, prop.t = FALSE, format = "SPSS") } When I try to use this function, however, I get the following error: #this results in 'Error: Must use a vector in `[`, not an object of class matrix.' data %>% select(col1, col2) %>% ctab() I tried searching online but couldn't find much about that error (except for in specific and unrelated cases). Moreover, when I created a very simple dataset, it turns out there's no problem: #this runs fine data.frame(C1 = c('x','y','x','y'), C2 = c('a','a','b','b')) %>% ctab() Is this a problem with my function or the data? If it's the data, why does directly calling CrossTable work? Thanks! Best, Zach [[alternative HTML version deleted]]
On 20/09/2019 11:30 a.m., Zachary Lim wrote:> Hi, > > I'm trying to create a simple function that takes a dataframe as its only argument. I've been using gmodels::CrossTable, but it requires a lot of arguments, e.g.: > > #this runs fine > CrossTable(data$col1, data$col2, prop.chisq = FALSE, prop.c = FALSE, prop.t = FALSE, format = "SPSS") > > Moreover, I wanted to make it compatible with piping, so I decided to create the following function: > > ctab <- function(data) { > CrossTable(data[,1], data[,2], prop.chisq = FALSE, prop.c = FALSE, prop.t = FALSE, format = "SPSS") > } > > When I try to use this function, however, I get the following error: > > #this results in 'Error: Must use a vector in `[`, not an object of class matrix.' > data %>% select(col1, col2) %>% ctab() > > I tried searching online but couldn't find much about that error (except for in specific and unrelated cases). Moreover, when I created a very simple dataset, it turns out there's no problem: > > #this runs fine > data.frame(C1 = c('x','y','x','y'), C2 = c('a','a','b','b')) %>% ctab() > > > Is this a problem with my function or the data? If it's the data, why does directly calling CrossTable work?Presumably data %>% select(col1, col2) isn't giving you a dataframe. However, you haven't given us a reproducible example, so I can't tell you what it's doing. But that's where you should look. Duncan Murdoch
Hello, Something like this? ctab <- function(data) { gmodels::CrossTable(as.matrix(data), prop.chisq = FALSE, prop.c = FALSE, prop.t = FALSE, format = "SPSS") } mtcars %>% select(cyl, gear) %>% ctab() Hope this helps, Rui Barradas ?s 16:30 de 20/09/19, Zachary Lim escreveu:> Hi, > > I'm trying to create a simple function that takes a dataframe as its only argument. I've been using gmodels::CrossTable, but it requires a lot of arguments, e.g.: > > #this runs fine > CrossTable(data$col1, data$col2, prop.chisq = FALSE, prop.c = FALSE, prop.t = FALSE, format = "SPSS") > > Moreover, I wanted to make it compatible with piping, so I decided to create the following function: > > ctab <- function(data) { > CrossTable(data[,1], data[,2], prop.chisq = FALSE, prop.c = FALSE, prop.t = FALSE, format = "SPSS") > } > > When I try to use this function, however, I get the following error: > > #this results in 'Error: Must use a vector in `[`, not an object of class matrix.' > data %>% select(col1, col2) %>% ctab() > > I tried searching online but couldn't find much about that error (except for in specific and unrelated cases). Moreover, when I created a very simple dataset, it turns out there's no problem: > > #this runs fine > data.frame(C1 = c('x','y','x','y'), C2 = c('a','a','b','b')) %>% ctab() > > > Is this a problem with my function or the data? If it's the data, why does directly calling CrossTable work? > > Thanks! > > Best, > Zach > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
The dplyr::select function returns a special variety of data.frame called a tibble. The tibble has certain features designed to make it behave consistently when indexing is used. Specifically, the `[` operator always returns a tibble regardless of how many columns are indicated by the column index. This is unlike the conventional data frame which returns a vector when exactly one column is indicated by the column index, or a data.frame if more than one is indicated. A syntax that consistently yields a column vector with both tibbles and data.frames is dta[[ 1 ]] so ctab <- function(data) { CrossTable(data[[1]], data[[2]], prop.chisq = FALSE, prop.c = FALSE, prop.t = FALSE, format = "SPSS") } should work. On September 20, 2019 10:59:46 AM PDT, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:>On 20/09/2019 11:30 a.m., Zachary Lim wrote: >> Hi, >> >> I'm trying to create a simple function that takes a dataframe as its >only argument. I've been using gmodels::CrossTable, but it requires a >lot of arguments, e.g.: >> >> #this runs fine >> CrossTable(data$col1, data$col2, prop.chisq = FALSE, prop.c = FALSE, >prop.t = FALSE, format = "SPSS") >> >> Moreover, I wanted to make it compatible with piping, so I decided to >create the following function: >> >> ctab <- function(data) { >> CrossTable(data[,1], data[,2], prop.chisq = FALSE, prop.c = FALSE, >prop.t = FALSE, format = "SPSS") >> } >> >> When I try to use this function, however, I get the following error: >> >> #this results in 'Error: Must use a vector in `[`, not an object of >class matrix.' >> data %>% select(col1, col2) %>% ctab() >> >> I tried searching online but couldn't find much about that error >(except for in specific and unrelated cases). Moreover, when I created >a very simple dataset, it turns out there's no problem: >> >> #this runs fine >> data.frame(C1 = c('x','y','x','y'), C2 = c('a','a','b','b')) %>% >ctab() >> >> >> Is this a problem with my function or the data? If it's the data, why >does directly calling CrossTable work? > >Presumably data %>% select(col1, col2) isn't giving you a dataframe. >However, you haven't given us a reproducible example, so I can't tell >you what it's doing. But that's where you should look. > >Duncan Murdoch > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.-- Sent from my phone. Please excuse my brevity.