如何从 R 中带有列联表的文件中读取维度名称

问题描述 投票:0回答:1

我有一个输入数据文件(比如 pets.csv)作为列联表

比如,所以

          Animal
        Dog    Cat
Color
Black    15   20
White    30   60

因此,维度名称是 Animal 和 Color。有没有办法用维度名称将其读入 R?

目前,我简单地输入文件

       Dog    Cat
Black    15   20
White    30   60

mytable<-read.csv("pets.csv", header = TRUE, row.names=1)

但这会丢失这些变量的名称(

Color
Animal)
。 我看到我可以将文件完全输入为数字并在 dimnames 中输入名称,但我宁愿只读取文件。我在文件中得到大量表格。

r dimensions contingency
1个回答
0
投票

我想你必须为此制作一个自定义功能。假设我们已将上面的表格复制并粘贴到 Excel 中,并将其另存为

animal_colors.csv
。然后你可以设置一个这样的功能:

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)
}

当我在如上设置的 CSV 文件上调用

read_ctable('animal_colors.csv')
时,这是输出:

       Animal
Color   Dog Cat
  Black  15  20
  White  30  60

这是一个

array
对象,在 IMO 中,它不如您通过调用
read.csv
获得的数据框对象有用,但您可以对其调用
as.data.frame
以获得整洁的数据框以供进一步分析:

  Color Animal Freq
1 Black    Dog   15
2 White    Dog   30
3 Black    Cat   20
4 White    Cat   60
© www.soinside.com 2019 - 2024. All rights reserved.