0

The Background:

I have a dataset that I’ve been given. The goal is to find out who’s been slacking on submitting their important forms.

I have the information (the names have been changed to protect the innocent, as well as if I make a mistake).

The data is separated into three columns; Business, Forms.Presented and Form.Title.

The Issue

When I run the code (below) I get a beautiful view of how I want it to look. This gives me separate tables based on Form.Title, so I can compare each individual form that should be presented by the business.

However, if I attempt to write.csv, write.table or write.xlsx, it gives me the data a different way.

Working:

> addmargins(with (Business_Forms, table (Business, Form.Presented., Form.Title)))

Funky:

write.xlsx(addmargins(with(Business_Forms, table(Business, Form.Presented.,Form.Title))), file = "C:/Users/Me/Desktop/Business.xlsx")

write.table(addmargins(with(Business_Forms, table(Business, Form.Presented.,Form.Title))), file = “clipboard”, sep= ”\t”)

The Funkiness:

What I expect to see is a simple, small set of tables based on the Form.Title field; exactly what I see in the console view.

Console view:

, , Form.Title = Any Known Allergens

        Form.Presented.
Business FALSE TRUE  Sum
  Food       0 1115 1115
  Planes     0    0    0
  Soda       0    0    0
  Trains     0    0    0
  Trucks     0    0    0
  Water      1    0    1
  Sum        1 1115 1116

, , Form.Title = Fuel Type

        Form.Presented.
Business. FALSE. TRUE.  Sum.
  Food       0    0    0
  Planes     0    0    0
  Soda       0    0    0
  Trains     3  204  207
  Trucks     0  172  172
  Water      0    0    0
  Sum        3  376  379

However, something’s happening between the console view and the spreadsheet. It runs and saves like it should, but instead of getting counts or frequencies broken down as I see them, it gets lumped differently.

Business    Form.Presented. Form.Title  Freq
Food        FALSE       Any Known Allergens 0
Planes      FALSE       Any Known Allergens 0
Soda        FALSE       Any Known Allergens 0
Trains      FALSE       Any Known Allergens 0
Trucks      FALSE       Any Known Allergens 0
Water       FALSE       Any Known Allergens 1
Sum         FALSE       Any Known Allergens 1
Food        TRUE        Any Known Allergens 1115

I can make it work by manipulating the information in excel, but that’s not why I’m using R.

Any advice would be greatly appreciated!

Update

I created a dataframe so the results can be replicated.

data1 <- c('Food','Water','Soda','Planes','Trains','Trucks')
data2 <- c('TRUE','FALSE','FALSE','TRUE','TRUE','TRUE')
data3 <- c('Safe For Moms','Any Known Allergens','Safe For Moms','MSDS/SDS','Offgas Fumes','Fuel Type')

Business<-c(data1,data1,data1,data1)
Form.Presented. <- c(data2,data2,data2,data2)
Form.Title <- c(data3,data3,data3,data3)

Business_Forms<-data.frame(Business,Form.Title,Form.Presented.)

If you run the code from before, it should work. I did test it before I typed this.

Willow44
  • 1
  • 1
  • 3
    If you could post a reproducible example, then we can try to run the code on our local computers and see the problem for ourselves. A reproducible example illustrates your main issue and allows others to recreate the problem on their computer. – Paul Wildenhain May 01 '19 at 16:24
  • Take a look at [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for some tips on how to make a reproducible example – divibisan May 01 '19 at 16:47
  • Those write functions expect a normal 2D data table that they can make into a normal 2D spreadsheet. It looks like the format you have is more of a series of small tables designed for human readability. You might want to take a look at this question: if the format you see in the console is what you want, then `sink` might be the function you want to use: https://stackoverflow.com/questions/7096989/how-to-save-all-console-output-to-file-in-r – divibisan May 01 '19 at 16:50
  • Okay, I've updated my question at the bottom. However, I will try 'sink' and see what happens! – Willow44 May 01 '19 at 17:22

0 Answers0