【问题标题】:Create contingency table from csv in R从R中的csv创建列联表
【发布时间】:2015-11-11 11:23:58
【问题描述】:

我正在使用ca 包进行对应分析。我已使用author 数据进行分析,效果非常好。

library(ca)
head(author[,1:5])
                               a   b   c   d    e
three daughters (buck)       550 116 147 374 1015
drifters (michener)          515 109 172 311  827
lost world (clark)           590 112 181 265  940
east wind (buck)             557 129 128 343  996
farewell to arms (hemingway) 589  72 129 339  866
sound and fury 7 (faulkner)  541 109 136 228  763

str(author)
 num [1:12, 1:26] 550 515 590 557 589 541 517 592 576 557 ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:12] "three daughters (buck)" "drifters (michener)" "lost world (clark)" "east wind (buck)" ...
  ..$ : chr [1:26] "a" "b" "c" "d" ...

ca(author[,1:5])

 Principal inertias (eigenvalues):
           1        2        3        4       
Value      0.008122 0.001307 0.001072 0.000596
Percentage 73.19%   11.78%   9.66%    5.37%   

...

然后我尝试将author 数据写入 csv 并读取 csv 以再次执行分析。然后ca 不起作用。读取的 csv 文件的str 不同,不像列联表。因此,ca 函数会产生错误。

author1 <- read.csv("author.csv")
colnames(author1)[1] <- ""
head(author1[,1:5])
                                 a   b   c   d
1       three daughters (buck) 550 116 147 374
2          drifters (michener) 515 109 172 311
3           lost world (clark) 590 112 181 265
4             east wind (buck) 557 129 128 343
5 farewell to arms (hemingway) 589  72 129 339
6  sound and fury 7 (faulkner) 541 109 136 228

str(author1[,1:5])
'data.frame':   12 obs. of  5 variables:
 $  : Factor w/ 12 levels "asia (michener)",..: 12 2 6 3 4 11 10 9 5 8 ...
 $ a: int  550 515 590 557 589 541 517 592 576 557 ...
 $ b: int  116 109 112 129 72 109 96 151 120 97 ...
 $ c: int  147 172 181 128 129 136 127 251 136 145 ...
 $ d: int  374 311 265 343 339 228 356 238 404 354 ...

ca(author1[,1:5])
Error in sum(N) : invalid 'type' (character) of argument

我想知道是否有一个简单的解决方法可以将author1 转换为源author

【问题讨论】:

    标签: r ca


    【解决方案1】:

    作者的第一列实际上是行名,因此在 csv 中读取并将第一列的名称更改为“”是问题所在。

    这行得通。

    library(data.table)
    library(dplyr)
    library(ca)
    
    head(author[,1:5])
    
    write.csv(author, file="author.csv")
    author2 <- read.csv("author.csv")
    
    head(author2[,1:5]) # here to row names are numbers
                                 X   a   b   c   d
    1       three daughters (buck) 550 116 147 374
    2          drifters (michener) 515 109 172 311
    3           lost world (clark) 590 112 181 265
    4             east wind (buck) 557 129 128 343
    5 farewell to arms (hemingway) 589  72 129 339
    6  sound and fury 7 (faulkner) 541 109 136 228
    
    # set row names to be first column of the csv
    rownames(author2) <- author2$X
    
    # remove the first column
    author2 %>% select(-X) -> author2
    
    head(author2[,1:5]) # notice the row names have changed
    
                                   a   b   c   d    e
    three daughters (buck)       550 116 147 374 1015
    drifters (michener)          515 109 172 311  827
    lost world (clark)           590 112 181 265  940
    east wind (buck)             557 129 128 343  996
    farewell to arms (hemingway) 589  72 129 339  866
    sound and fury 7 (faulkner)  541 109 136 228  763
    

    【讨论】:

      猜你喜欢
      • 2020-10-06
      • 2020-05-02
      • 2016-03-03
      • 2016-12-29
      • 2013-08-15
      • 1970-01-01
      • 1970-01-01
      • 2020-11-02
      • 2016-03-12
      相关资源
      最近更新 更多