2

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.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
Zina Taran
  • 21
  • 2

1 Answers1

2

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:

read_ctable <- function(x) {
  # get the variable names from first two lines
  vars <- readLines(x, 2)  
  # remove comma separators
  vars <- gsub(',', '', vars)  
  # read in data starting on third line
  df <- read.csv(x, header=T, row.names=1, skip=2)
  # convert to table
  dt <- as.table(as.matrix(df))  
  # add variable names (have to reverse, because of the order in which they appeared in the file)
  names(attributes(dt)$dimnames) <- rev(vars)  
  return(dt)
}

When I call read_ctable('animal_colors.csv') on a CSV file set up as above, this is the output:

       Animal
Color   Dog Cat
  Black  15  20
  White  30  60

This is an array object, which IMO is less useful than the data frame object you get from just calling read.csv, but you can call as.data.frame on it to get a nice tidy data frame for further analysis:

  Color Animal Freq
1 Black    Dog   15
2 White    Dog   30
3 Black    Cat   20
4 White    Cat   60
C. Murtaugh
  • 574
  • 4
  • 15