I have an input data file (say, pets.csv)as a contingency table
As for example, so
Animal
Dog Cat
Color
Black 15 20
White 30 60
So, the dimension names are Animal and Color. Is there a way to read this into R with the dimension names?
Currently, I input the file as simply
Dog Cat
Black 15 20
White 30 60
and do
mytable<-read.csv("pets.csv", header = TRUE, row.names=1)
But that loses the names of these variables (Color and Animal).
I see that I can input the file completely as numbers and input the names in dimnames, but I would rather just read the file. I get tons of tables in files.
I think you would have to make a custom function for this. Let's assume we have copied and pasted your table, above, into Excel, and saved it as
animal_colors.csv. Then you could set up a function like this:When I call
read_ctable('animal_colors.csv')on a CSV file set up as above, this is the output:This is an
arrayobject, which IMO is less useful than the data frame object you get from just callingread.csv, but you can callas.data.frameon it to get a nice tidy data frame for further analysis: