0

Data example:

s2_nnormalefietsen_voor<- c("2+", "1", "0", "2+", "1", "0", "0", "1", "1", "0")
s2_nnormalefietsen_nu<- c("2+", "2+", "0", "2+", "1", "1", "0", "1", "0", "2+")

nnormalefietsen<- data.frame(s2_nnormalefietsen_voor, s2_nnormalefietsen_nu)

I have this code used to create a crosstab:

source("http://pcwww.liv.ac.uk/~william/R/crosstab.r")
crosstabs_nnormalefietsen <- crosstab(nnormalefietsen, row.vars = "s2_nnormalefietsen_voor", col.vars = "s2_nnormalefietsen_nu", type = "t")
crosstabs_nnormalefietsen <- crosstabs_nnormalefietsen$table/1961

write.csv(crosstabs_nnormalefietsen,"crosstabs_nnormalefietsen.csv")

This code:

crosstab(nnormalefietsen, row.vars = "s2_nnormalefietsen_voor", col.vars = "s2_nnormalefietsen_nu", type = "t")

Returns this:

                        s2_nnormalefietsen_nu      0      1     2+    Sum
s2_nnormalefietsen_voor                                                  
0                                               8.61   0.57   0.26   9.44
1                                               0.73  20.24   2.40  23.37
2+                                              0.63   1.77  64.79  67.19
Sum                                             9.96  22.59  67.45 100.00

But this code:

crosstabs_nnormalefietsen <- crosstab(nnormalefietsen, row.vars = "s2_nnormalefietsen_voor", col.vars = "s2_nnormalefietsen_nu", type = "t")
crosstabs_nnormalefietsen <- crosstabs_nnormalefietsen$table/1961

write.csv(crosstabs_nnormalefietsen,"crosstabs_nnormalefietsen.csv")

Returns this:

enter image description here

I understand that it might move the decimal point to points, but why do the actual numbers change?

Victor Nielsen
  • 443
  • 2
  • 14
  • Please provide a self contained minimal reproducible example. Downloading and running large amounts of untrusted code from the net is not safe. – G. Grothendieck Apr 05 '22 at 09:39
  • @G. Grothendieck The source code is the crosstab function, not the data, but let me see if I can find a way to make the data. – Victor Nielsen Apr 05 '22 at 10:35
  • @G.Grothendieck I added data. – Victor Nielsen Apr 05 '22 at 10:38
  • The question should have complete minimal self contained code and data that anyone can easily copy and paste into their session. See the top of the [tag:r] tag page. – G. Grothendieck Apr 05 '22 at 14:20
  • How is that different from what I did on the top of my post? @G.Grothendieck. Is there not enough data? – Victor Nielsen Apr 05 '22 at 14:24
  • The code is not self contained. It depends on running a large amount of external code which could be unsafe. – G. Grothendieck Apr 05 '22 at 14:47
  • @G.Grothendieck But the whole post is about this crosstab function. How can I possibly ask my question without using it? – Victor Nielsen Apr 06 '22 at 10:21
  • @G.Grothendieck does it help if I show where I got the function from? http://rstudio-pubs-static.s3.amazonaws.com/6975_c4943349b6174f448104a5513fed59a9.html - this is the first Google result when you seach "cross tabulation r" – Victor Nielsen Apr 06 '22 at 12:58
  • @G.Grothendieck They didn't have a problem with this one; https://stackoverflow.com/questions/38776110/crosstable-export-to-csv – Victor Nielsen Apr 06 '22 at 13:05
  • In that case either someone was willing to take the chance or else they managed to answer it without running the code. – G. Grothendieck Apr 06 '22 at 13:09

0 Answers0