轻拍()?等长?

tapply ()? Equal lengths?

下午好, 我正在尝试应用 tapply 函数,以便通过以下数据集的不同治疗组('Placebo' 组和 'Active' 组)获取平均值:

> str(dat_long)
'data.frame':   1500 obs. of  7 variables:
 $ subject.id: num  1 1 1 2 2 2 3 3 3 4 ...
 $ treatment : Factor w/ 2 levels "Placebo","Active": 1 1 1 1 1 1 1 1 1 1 ...
 $ sex       : num  1 1 1 1 1 1 1 1 1 1 ...
 $ age       : num  58.1 58.1 58.1 54.8 54.8 ...
 $ miss_pat  : chr  "---" "---" "---" "--X" ...
 $ times     : num  1 2 3 1 2 3 1 2 3 1 ...
 $ scores    : num  13.62 7.25 20.45 33.34 20.9 ..

我正在处理的数据集格式是“长”格式。我创建了以下列表对象

flst <- list(times, treatment)

通过收集不同的时间点和接受的治疗,其中我 运行 tapply() 函数。

(tN <-  
    tapply(scores, flst, 
              FUN = function(x) length(x[!is.na(x)])))

我不明白为什么我一直在找回同样的错误

Error in tapply(scores, flst, FUN = function(x) length(x[!is.na(x)])) : 
  arguments must have the same length

我试图寻找解决方案(例如,作为因子变量进行隐藏等),但其中 none 似乎适合我的情况。有人可能知道我要解决的问题吗?

为了以防万一,为了应对 NA 观察,我应该在代码中输入什么以及在哪里输入?

非常感谢关注

P.S。以防万一我在这里报告每个参数 lengths

> length(flst)
[1] 2
> length(scores)
[1] 1500

好的。 感谢到目前为止的回答。我将尝试提供更多细节,只是为了让情况更容易理解。

这是我正在处理的原始数据集,采用宽格式。

> dat_wide
    subject.id treatment measure1  measure2 measure3 sex      age miss_pat
1            1         1 13.61906  7.249175 20.44918   1 58.12831      ---
2            2         1 33.33751 20.896999       NA   1 54.77794      --X
3            3         1       NA 20.869364 26.12443   1 59.55550      X--
4            4         1 37.75143 26.253406 29.03896   1 59.37837      ---
5            5         1 28.21168 14.652736 23.65234   1 63.34226      ---
6            6         1 32.40856 18.702288       NA   0 55.36262      --X
7            7         1 31.63005 18.682889       NA   1 59.02119      --X
8            8         1       NA 18.244421 22.02274   0 57.57435      X--
9            9         1       NA 27.351230 36.86480   1 52.08140      X--
10          10         1 28.10867 16.769758 23.67930   1 62.93165      ---
11          11         1 33.55693 18.499841       NA   0 53.46754      --X
12          12         1 22.88379 14.061066 30.02255   1 57.49554      ---
13          13         1 30.99073 15.350301 20.69450   1 57.92600      ---
14          14         1 26.35118 15.195037 26.83573   0 58.42347      ---
15          15         1 20.80077 16.112581 34.78064   1 60.22746      ---
16          16         1 34.64003        NA 32.96944   1 56.28172      -X-
17          17         1 26.70361 23.846794 31.24356   0 57.93085      ---
18          18         1 25.61433 19.779788 25.75783   1 59.35028      ---
19          19         1 36.51201 24.594663       NA   0 59.32188      --X
20          20         1 33.18090 31.846358 34.21801   0 58.16217      ---
21          21         1       NA 23.813420 28.49156   1 58.39313      X--
22          22         1 28.20934 12.516007       NA   0 53.24436      --X
23          23         1 30.64196        NA 35.01832   1 59.12392      -X-
24          24         1       NA 26.596392       NA   1 56.22792      X-X
25          25         1       NA 13.380347 27.72617   1 59.75437      X--
26          26         1 27.95115 14.652209 27.44068   1 64.86220      ---
27          27         1 31.01423 18.881365 30.15189   0 61.84734      ---
28          28         1 41.28848 32.966200       NA   0 60.00848      --X
29          29         1 23.90214 16.894180 30.08366   0 55.09433      ---
30          30         1 27.91571 21.604654 29.97184   0 57.16355      ---
31          31         1 25.48024 14.512467 26.89893   1 52.28091      ---
32          32         1       NA 20.627647 24.94113   1 59.14089      X--
33          33         1       NA        NA 30.30841   0 63.53035      XX-
34          34         1       NA 17.220079 27.11605   0 54.70157      X--
35          35         1       NA 22.904332 30.54679   0 62.82148      X--
36          36         1 24.71682 11.343220 20.16185   0 55.81194      ---
37          37         1 21.54434 15.245485 27.42116   0 56.77520      ---
38          38         1 31.00812 21.882236 31.04285   1 53.60319      ---
39          39         1       NA 29.445792       NA   0 62.88837      X-X
40          40         1       NA 21.325835 27.29858   0 59.92034      X--
41          41         1       NA 23.330492 31.01700   1 57.10396      X--
42          42         1 22.13132 17.385187       NA   1 49.80586      --X
43          43         1       NA 17.181349 24.70715   1 56.92603      X--
44          44         1 24.57225        NA 32.11101   1 52.17824      -X-
45          45         1 28.79543 15.302079 24.03278   1 63.82524      ---
46          46         1 25.36835 18.152139 23.56443   1 56.89171      ---
47          47         1       NA 21.594259       NA   1 56.04408      X-X
48          48         1 26.62196 22.544339 27.04008   1 62.31757      ---
49          49         1 24.22001  6.924847 19.89172   0 58.69229      ---
50          50         1 34.14105 22.283254 34.41771   1 57.73471      ---
51          51         1 18.90136 13.449724 28.69964   0 63.48494      ---
52          52         1 33.01857 20.880303 31.32787   1 60.22318      ---
53          53         1 31.06980 23.687328 32.66611   1 54.40638      ---
54          54         1 31.98485 20.204848 26.00955   0 62.71068      ---
55          55         1 30.50251 19.632418 19.03141   1 59.27135      ---
56          56         1       NA 24.607656 30.37130   1 59.40362      X--
57          57         1 33.55517 22.213067 27.59768   0 57.04789      ---
58          58         1 24.19856 10.095334 21.76153   0 54.56281      ---
59          59         1 29.62803 15.703452 26.99926   0 56.66213      ---
60          60         1 34.01922 18.515003       NA   0 52.76635      --X
61          61         1 29.86470 17.587567 26.02713   1 61.06847      ---
62          62         1 25.61215 20.746458 25.80680   1 64.25965      ---
63          63         1 27.60233 16.047261 27.63316   1 60.87826      ---
64          64         1 24.18180 15.007888       NA   1 62.95863      --X
65          65         1 30.34542 24.986253 32.33394   0 52.55152      ---
66          66         1 21.53614 15.999301 27.95432   1 57.96632      ---
67          67         1       NA 27.856354       NA   1 61.76696      X-X
68          68         1 22.09629 15.252170 28.43995   0 64.99414      ---
69          69         1 25.80952 18.565819 27.77111   0 61.26983      ---
70          70         1 32.99136 22.610543       NA   0 57.79422      --X
71          71         1       NA 20.701995 23.77574   0 59.83622      X--
72          72         1 25.53110 18.466437 29.50384   1 60.38576      ---
73          73         1 32.21151 21.740227 24.21088   1 56.07185      ---
74          74         1 21.28635  9.444369 18.28459   0 50.20183      ---
75          75         1       NA 25.000759 33.57668   1 50.22598      X--
76          76         1 26.12087 12.385796 18.91420   1 53.40782      ---
77          77         1       NA 22.767420 28.75717   1 48.27044      X--
78          78         1 24.64162 21.218430       NA   1 55.94655      --X
79          79         1 27.07266 15.348420 24.72210   0 53.69175      ---
80          80         1 32.75587 28.013513       NA   0 58.04689      --X
81          81         1 29.30842 17.072955 24.29882   1 61.06444      ---
82          82         1       NA 18.382335 28.69544   0 58.54447      X--
83          83         1 31.78905 24.603102 33.91789   0 56.24937      ---
84          84         1 27.99531 19.921259 25.14260   1 58.49543      ---
85          85         1 26.34945 17.718344 33.91154   1 65.19380      ---
86          86         1 31.28590 18.000405 29.33850   1 64.79698      ---
87          87         1 19.46943 12.058767 24.16417   0 61.99136      ---
88          88         1 33.05861 27.022073 35.98174   1 57.79309      ---
89          89         1 25.82099 14.353354 22.29782   1 56.99255      ---
90          90         1       NA 18.394579 25.89987   0 52.29756      X--
91          91         1       NA 17.594238       NA   0 57.60890      X-X
92          92         1 36.62178 25.121140 34.90862   1 59.53585      ---
93          93         1 31.58383 16.929367 18.06532   1 56.49771      ---
94          94         1 32.79600 25.161476 29.57509   1 58.27547      ---
95          95         1 32.57059 23.029130       NA   1 58.93147      --X
96          96         1 32.44934 12.892612       NA   1 58.54679      --X
97          97         1 28.83562 15.361608 21.96002   0 52.01731      ---
98          98         1 28.49706 22.351217       NA   1 58.02899      --X
99          99         1 25.28790 14.283020       NA   0 53.90112      --X
100        100         1       NA 20.031777 29.93904   0 59.34664      X--
101        101         1 30.86933 20.853957 23.67089   1 60.41797      ---
102        102         1       NA        NA 25.25865   1 58.51619      XX-
103        103         1 32.60158 23.890740 27.78209   1 52.93076      ---
104        104         1 29.64438 26.275628 32.96612   0 57.67552      ---
105        105         1 19.47877 10.237089 14.53323   1 62.56323      ---
106        106         1 28.50985 24.172877       NA   1 56.20708      --X
107        107         1       NA 21.111304 27.44881   1 55.47168      X--
108        108         1 26.90495 15.140661 26.54333   0 59.25477      ---
109        109         1       NA 28.740031       NA   0 53.61378      X-X
110        110         1 27.55861 21.708753 30.61987   0 62.90328      ---
111        111         1       NA 26.379662       NA   1 55.03740      X-X
112        112         1 22.88802 13.638065 25.35349   0 57.83266      ---
113        113         1 32.92606 24.907022 27.52318   0 62.04887      ---
114        114         1       NA 23.853791 34.18453   1 60.97984      X--
115        115         1       NA 32.755543 34.09703   1 60.03185      X--
116        116         1 17.04010 10.772446 22.38210   1 56.68107      ---
117        117         1 20.80304 16.040884 19.17766   0 60.20649      ---
118        118         1       NA 15.337750 28.42040   0 58.86986      X--
119        119         1 26.22063  9.210329 16.85208   1 61.22018      ---
120        120         1 39.27848 21.273545 29.96761   0 57.69893      ---
121        121         1       NA 20.177623 27.03394   1 58.71332      X--
122        122         1       NA 19.084165 26.29572   1 61.85389      X--
123        123         1 25.33397 22.082934 35.30603   0 55.82552      ---
124        124         1 27.30526 22.526807 33.81731   1 58.29532      ---
125        125         1       NA 26.020347 35.15877   1 66.13519      X--

进一步更改后,我使用 tidyr 库的函数 gather 将其转换为长格式,并且我对每个观察进行了排序,以便为三个测量时间中的每一个排序主题列-点.

> dat_long
     subject.id treatment sex      age miss_pat times    scores
1             1   Placebo   1 58.12831      ---     1 13.619060
501           1   Placebo   1 58.12831      ---     2  7.249175
1001          1   Placebo   1 58.12831      ---     3 20.449176
2             2   Placebo   1 54.77794      --X     1 33.337510
502           2   Placebo   1 54.77794      --X     2 20.896999
1002          2   Placebo   1 54.77794      --X     3        NA
3             3   Placebo   1 59.55550      X--     1        NA
503           3   Placebo   1 59.55550      X--     2 20.869364
1003          3   Placebo   1 59.55550      X--     3 26.124432
4             4   Placebo   1 59.37837      ---     1 37.751430
504           4   Placebo   1 59.37837      ---     2 26.253406
1004          4   Placebo   1 59.37837      ---     3 29.038963
5             5   Placebo   1 63.34226      ---     1 28.211679
505           5   Placebo   1 63.34226      ---     2 14.652736
1005          5   Placebo   1 63.34226      ---     3 23.652342
6             6   Placebo   0 55.36262      --X     1 32.408561
506           6   Placebo   0 55.36262      --X     2 18.702288
1006          6   Placebo   0 55.36262      --X     3        NA
7             7   Placebo   1 59.02119      --X     1 31.630050
507           7   Placebo   1 59.02119      --X     2 18.682889
1007          7   Placebo   1 59.02119      --X     3        NA
8             8   Placebo   0 57.57435      X--     1        NA
508           8   Placebo   0 57.57435      X--     2 18.244421
1008          8   Placebo   0 57.57435      X--     3 22.022740
9             9   Placebo   1 52.08140      X--     1        NA
509           9   Placebo   1 52.08140      X--     2 27.351230
1009          9   Placebo   1 52.08140      X--     3 36.864796
10           10   Placebo   1 62.93165      ---     1 28.108672
510          10   Placebo   1 62.93165      ---     2 16.769758
1010         10   Placebo   1 62.93165      ---     3 23.679297
11           11   Placebo   0 53.46754      --X     1 33.556930
511          11   Placebo   0 53.46754      --X     2 18.499841
1011         11   Placebo   0 53.46754      --X     3        NA
12           12   Placebo   1 57.49554      ---     1 22.883795
512          12   Placebo   1 57.49554      ---     2 14.061066
1012         12   Placebo   1 57.49554      ---     3 30.022550
13           13   Placebo   1 57.92600      ---     1 30.990728
513          13   Placebo   1 57.92600      ---     2 15.350301
1013         13   Placebo   1 57.92600      ---     3 20.694504
14           14   Placebo   0 58.42347      ---     1 26.351176
514          14   Placebo   0 58.42347      ---     2 15.195037
1014         14   Placebo   0 58.42347      ---     3 26.835728
15           15   Placebo   1 60.22746      ---     1 20.800769
515          15   Placebo   1 60.22746      ---     2 16.112581
1015         15   Placebo   1 60.22746      ---     3 34.780644
16           16   Placebo   1 56.28172      -X-     1 34.640032
516          16   Placebo   1 56.28172      -X-     2        NA
1016         16   Placebo   1 56.28172      -X-     3 32.969443
17           17   Placebo   0 57.93085      ---     1 26.703609
517          17   Placebo   0 57.93085      ---     2 23.846794
1017         17   Placebo   0 57.93085      ---     3 31.243559
18           18   Placebo   1 59.35028      ---     1 25.614333
518          18   Placebo   1 59.35028      ---     2 19.779788
1018         18   Placebo   1 59.35028      ---     3 25.757833
19           19   Placebo   0 59.32188      --X     1 36.512015
519          19   Placebo   0 59.32188      --X     2 24.594663
1019         19   Placebo   0 59.32188      --X     3        NA
20           20   Placebo   0 58.16217      ---     1 33.180896
520          20   Placebo   0 58.16217      ---     2 31.846358
1020         20   Placebo   0 58.16217      ---     3 34.218013
21           21   Placebo   1 58.39313      X--     1        NA
521          21   Placebo   1 58.39313      X--     2 23.813420
1021         21   Placebo   1 58.39313      X--     3 28.491556
22           22   Placebo   0 53.24436      --X     1 28.209340
522          22   Placebo   0 53.24436      --X     2 12.516007
1022         22   Placebo   0 53.24436      --X     3        NA
23           23   Placebo   1 59.12392      -X-     1 30.641962
523          23   Placebo   1 59.12392      -X-     2        NA
1023         23   Placebo   1 59.12392      -X-     3 35.018324
24           24   Placebo   1 56.22792      X-X     1        NA
524          24   Placebo   1 56.22792      X-X     2 26.596392
1024         24   Placebo   1 56.22792      X-X     3        NA
25           25   Placebo   1 59.75437      X--     1        NA
525          25   Placebo   1 59.75437      X--     2 13.380347
1025         25   Placebo   1 59.75437      X--     3 27.726174
26           26   Placebo   1 64.86220      ---     1 27.951153
526          26   Placebo   1 64.86220      ---     2 14.652209
1026         26   Placebo   1 64.86220      ---     3 27.440682
27           27   Placebo   0 61.84734      ---     1 31.014231
527          27   Placebo   0 61.84734      ---     2 18.881365
1027         27   Placebo   0 61.84734      ---     3 30.151889
28           28   Placebo   0 60.00848      --X     1 41.288482
528          28   Placebo   0 60.00848      --X     2 32.966200
1028         28   Placebo   0 60.00848      --X     3        NA
29           29   Placebo   0 55.09433      ---     1 23.902144
529          29   Placebo   0 55.09433      ---     2 16.894180
1029         29   Placebo   0 55.09433      ---     3 30.083663
30           30   Placebo   0 57.16355      ---     1 27.915708
530          30   Placebo   0 57.16355      ---     2 21.604654
1030         30   Placebo   0 57.16355      ---     3 29.971840
31           31   Placebo   1 52.28091      ---     1 25.480237
531          31   Placebo   1 52.28091      ---     2 14.512467
1031         31   Placebo   1 52.28091      ---     3 26.898932
32           32   Placebo   1 59.14089      X--     1        NA
532          32   Placebo   1 59.14089      X--     2 20.627647
1032         32   Placebo   1 59.14089      X--     3 24.941132
33           33   Placebo   0 63.53035      XX-     1        NA
533          33   Placebo   0 63.53035      XX-     2        NA
1033         33   Placebo   0 63.53035      XX-     3 30.308410
34           34   Placebo   0 54.70157      X--     1        NA
534          34   Placebo   0 54.70157      X--     2 17.220079
1034         34   Placebo   0 54.70157      X--     3 27.116048
35           35   Placebo   0 62.82148      X--     1        NA
535          35   Placebo   0 62.82148      X--     2 22.904332
1035         35   Placebo   0 62.82148      X--     3 30.546789
36           36   Placebo   0 55.81194      ---     1 24.716824
536          36   Placebo   0 55.81194      ---     2 11.343220
1036         36   Placebo   0 55.81194      ---     3 20.161854
37           37   Placebo   0 56.77520      ---     1 21.544340
537          37   Placebo   0 56.77520      ---     2 15.245485
1037         37   Placebo   0 56.77520      ---     3 27.421159
38           38   Placebo   1 53.60319      ---     1 31.008123
538          38   Placebo   1 53.60319      ---     2 21.882236
1038         38   Placebo   1 53.60319      ---     3 31.042851
39           39   Placebo   0 62.88837      X-X     1        NA
539          39   Placebo   0 62.88837      X-X     2 29.445792
1039         39   Placebo   0 62.88837      X-X     3        NA
40           40   Placebo   0 59.92034      X--     1        NA
540          40   Placebo   0 59.92034      X--     2 21.325835
1040         40   Placebo   0 59.92034      X--     3 27.298584
41           41   Placebo   1 57.10396      X--     1        NA
541          41   Placebo   1 57.10396      X--     2 23.330492
1041         41   Placebo   1 57.10396      X--     3 31.017001
42           42   Placebo   1 49.80586      --X     1 22.131323
542          42   Placebo   1 49.80586      --X     2 17.385187
1042         42   Placebo   1 49.80586      --X     3        NA
43           43   Placebo   1 56.92603      X--     1        NA
543          43   Placebo   1 56.92603      X--     2 17.181349
1043         43   Placebo   1 56.92603      X--     3 24.707149
44           44   Placebo   1 52.17824      -X-     1 24.572247
544          44   Placebo   1 52.17824      -X-     2        NA
1044         44   Placebo   1 52.17824      -X-     3 32.111015
45           45   Placebo   1 63.82524      ---     1 28.795431
545          45   Placebo   1 63.82524      ---     2 15.302079
1045         45   Placebo   1 63.82524      ---     3 24.032779
46           46   Placebo   1 56.89171      ---     1 25.368353
546          46   Placebo   1 56.89171      ---     2 18.152139
1046         46   Placebo   1 56.89171      ---     3 23.564433
47           47   Placebo   1 56.04408      X-X     1        NA
547          47   Placebo   1 56.04408      X-X     2 21.594259
1047         47   Placebo   1 56.04408      X-X     3        NA
48           48   Placebo   1 62.31757      ---     1 26.621959

我将构建一种 table,其中报告了样本量 (tN)、平均值 (tMn) 和中值 (tMd)。为此,我打算使用以下代码

attach(dat_long)
flst <- list(times, treatment)
(tN <-  
    tapply(visual, flst, 
              FUN = function(x) length(x[!is.na(x)])))

(tMn <- tapply(scores, flst, FUN = mean))     
(tMd <- tapply(scores, flst, FUN = median))  
colnames(res <- cbind(tN, tMn, tMd))
nms1 <- rep(c("P", "A"), 3)
nms2 <- rep(c("n", "Mean", "Mdn"), rep(2, 3)) #n = numerosità
colnames(res) <- paste(nms1, nms2, sep = ":")
res

我认为不会消除可能的疑虑。请让我知道如何处理这个问题。

谢谢