NickCH-K / vtable

An R package for creating Variable Documentation Files

Home Page:https://nickch-k.github.io/vtable/index.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Sumtable Vars redux

qvrb opened this issue · comments

commented

when i try to call sumtable im getting the error Error in round(results[y], digits = max(digits[y], 1)) :
non-numeric argument to mathematical function

the failed call st(data,vars = c('responses', 'offers', 'bestoffer','meanoffer','name','polite','price','black'))
if i call st(data,vars = c('responses', 'offers', 'bestoffer','meanoffer','name','polite','price')) it works fine

so that implies that black is non-numeric except
str(data$black) returns TRUE

dbl+lbl [1:1200] 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1,...
@ label : chr "Black"
@ format.stata: chr "%9.0g"
@ labels : Named num [1:2] 0 1
..- attr(*, "names")= chr [1:2] "Other" "Black"

head(data)

head(data)
ad responses offers bestoffer meanoffer name polite price texttype black tattoo white
1: 55 0 0 NA NA NA NA 130 0 0 0 1
2: 59 1 1 95 95.0 1.0 1.0 110 0 0 0 1
3: 60 2 0 NA NA 1.0 1.0 90 0 1 0 0
4: 65 0 0 NA NA NA NA 110 0 0 0 1
5: 66 5 2 90 82.5 0.2 0.6 90 0 1 0 0
6: 67 2 1 75 75.0 0.0 0.0 110 0 1 0 0

Hard for me to help much without having the data on hand. But it looks like the issue is the use of value labels, which the package does try to support if they come from the haven package but also can cause shenanigans in corner cases. In your particular case I'd imagine you could fix the problem by converting black to a proper factor variable, I think sjlabelled::as_factor() does this.

commented

Hi,
I've attached the sta file but renamed it as a csv as github won't allow sta uploads
Thanks a million for your prompt response
Regards

commented

library(haven library(vtable) library(sjlabelled) data <- read_dta("c:\\temp\\data_doleacstein.dta") st(data,vars = c('responses', 'offers', 'bestoffer','meanoffer','name','polite','price','offers',sjlabelled::as_factor('black'))))
which gives me
Rplot
which is not quite what i was expecting since i get a 1 for the data in the black label

That CSV file is corrupted and won't work; it looks like you just changed the file extension to CSV instead of using export delimited or something. In any case, if you did convert to CSV I suspect the problem would go away. The problem here is definitely the use of haven and it importing value labels from a Stata file, which wouldn't happen with a CSV.

Your recent post doesn't work because vars only accepts the names of columns, you can't do calculations in it. If you first create hte factor variable, using perhaps mutate(factor_black = sjlabelled::as_factor('black')) or something like that (if you're using tidyverse), and then using 'factor_black' in your vars argument, I suspect that would work.