【问题标题】:Loosing observation when I use reshape in R当我在 R 中使用 reshape 时失去观察
【发布时间】:2016-06-22 04:09:45
【问题描述】:

我有数据集

> head(pain_subset2, n= 50)
     PatientID RSE SE SECODE
1    1001-01   0  0      0
2    1001-01   0  0      0
3    1001-02   0  0      0
4    1001-02   0  0      0
5    1002-01   0  0      0
6    1002-01   1 2a      1
7    1002-02   0  0      0
8    1002-02   0  0      0
9    1002-02   0  0      0
10   1002-03   0  0      0
11   1002-03   0  0      0
12   1002-03   1  1      1

> dim(pain_subset2)
[1] 817   4
> table(pain_subset2$RSE)
  0   1 
788  29 
> table(pain_subset2$SE)
  0   1  2a  2b   3   4   5 
788   7   5   1   6   4   6 
> table(pain_subset2$SECODE)
  0   1 
788  29 

我想创建 n * 6 的矩阵(n :# of PatientID, column :6 个 SE 级别) 我使用 reshape,我失去了很多观察结果

> dim(p)
[1] 246   9

我的代码:

p <- reshape(pain_subset2, timevar = "SE", idvar =  c("PatientID","RSE"),v.names = "SECODE", direction = "wide")
p[is.na(p)] <- 0
> table(p$RSE)
  0   1 
226  20 

与 RSE 表相比,我失去了 9 名患者有 1 名。

这是我的输出

    PatientID RSE SECODE.0 SECODE.2a SECODE.1 SECODE.5 SECODE.3 SECODE.2b  SECODE.4
1     1001-01   0        0         0        0        0        0         0           0
3     1001-02   0        0         0        0        0        0         0        0
5     1002-01   0        0         0        0        0        0         0        0
6     1002-01   1        0         1        0        0        0         0        0
7     1002-02   0        0         0        0        0        0         0        0
10    1002-03   0        0         0        0        0        0         0        0
12    1002-03   1        0         0        1        0        0         0        0
13    1002-04   0        0         0        0        0        0         0        0
15    1003-01   0        0         0        0        0        0         0        0
18    1003-02   0        0         0        0        0        0         0        0
21    1003-03   0        0         0        0        0        0         0        0
24    1003-04   0        0         0        0        0        0         0        0
27    1003-05   0        0         0        0        0        0         0        0
30    1003-06   0        0         0        0        0        0         0        0
32    1003-07   0        0         0        0        0        0         0        0
35    1004-01   0        0         0        0        0        0         0        0
36    1004-01   1        0         0        0        1        0         0        0
40   1004-02a   0        0         0        0        0        0         0        0

任何人都知道会发生什么,我真的很感激。 谢谢你的帮助,最好的。

【问题讨论】:

    标签: r matrix reshape


    【解决方案1】:

    试试:

    library(dplyr)
    library(tidyr)
    
    pain_subset2 %>%
      spread(SE, SECODE)
    

    【讨论】:

    • 谢谢,我修好了,现在正确了,我再添加 1 个变量 idvar = c("PatientID","Eval",RSE"),所以让它独一无二,我得到了总维度跨度>
    • @Stat 哈哈,很高兴你明白了 :)
    • 仍然想不出创建矩阵的最佳方法
    • p 是一个数据框。请尝试as.matrix(p[, -(1:2)]) 强制除p 的前两列之外的所有列。
    • 我的意思是不使用reshape,矩阵将只有1和0,如果SE有2a,或2b,或1,6个级别,设置1,如果不设置0,我使用reshape然后我必须整理数据,所以我只能为每一行获得唯一的患者
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-27
    • 1970-01-01
    • 2021-09-25
    • 1970-01-01
    • 2017-09-06
    • 1970-01-01
    • 2021-11-23
    相关资源
    最近更新 更多