data.frame(...,check.names = FALSE)错误:参数暗示不同的行数:18、218

时间:2019-10-20 14:06:04

标签: r dataframe row

我想将脉率与左撇子还是右撇子相比较。问题在于,在我的数据中,左手只有18个人,右手是218个人。在尝试创建数据帧时,这是有问题的,因为两列的长度都不相同。该错误显示为:

Error in data.frame(..., check.names = FALSE) : arguments imply differing number of rows: 18, 218. 

有没有办法解决这个问题?

table(pulse)
pulse
 35  40  48  50  54  55  56  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  78  79 
  1   1   2   2   1   1   1   1  12   1   4   1   9   6   6   1  16   1  13   2  14   1   5   5  13   4   3 
 80  81  83  84  85  86  87  88  89  90  92  96  97  98 100 104 
 18   1   4   5   4   3   2   4   1   8   6   3   1   1   2   2 

table(whnd)
whnd
 Left Right 
   18   218 

left= collect[which(whnd=="Left"),]
left

left
    sex wrspan nwspan whnd fold pulse clap exer smoke height units    age
2     2   19.5   20.5    1    3   104    1    2     4 177.80     1 17.583
18    2   19.4   19.2    1    3    74    3    3     2 182.88     1 18.333
51    2   22.0   21.5    1    3    55    1    1     2 200.00     2 18.500
60    2   20.6   21.0    1    1    NA    1    1     3 175.26     1 18.417
64    1   18.7   18.0    1    1    NA    1    2     2 170.00     2 19.833
81    2   19.5   19.5    1    3    66    1    3     2     NA    NA 16.750
90    1   18.0   17.7    1    3    92    1    3     2     NA    NA 17.583
118   2   23.0   22.0    1    1    83    1    3     1 193.04     1 18.917
119   1   18.5   18.0    1    1   100    2    3     2 171.00     2 18.917
124   2   19.8   20.0    1    1    59    3    1     2 180.00     2 17.417
134   1   15.4   16.4    1    1    80    1    1     3 160.02     1 18.500
137  NA   19.8   19.0    1    1    73    2    1     2 172.00     2 21.500
145   1   20.0   19.5    1    3    68    2    1     2 172.00     2 19.167
162   2   18.1   18.2    1    2    NA    3    3     2 168.00     2 21.167
172   2   20.5   19.5    1    1    80    3    3     3 182.88     1 18.667
176   1   19.0   18.5    1    1   104    1    1     2 170.00     2 17.250
209   2   17.5   17.0    1    1    97    2    2     2 165.00     2 19.500
212   1   17.5   17.5    1    3    83    2    3     2 163.00     2 17.250
right= collect[which(whnd=="Right"),]
right

sex wrspan nwspan whnd fold pulse clap exer smoke height units    age
1     1   18.5   18.0    2    3    92    1    3     2 173.00     2 18.250
3     2   18.0   13.3    2    1    87    2    2     3     NA    NA 16.917
4     2   18.8   18.9    2    3    NA    2    2     2 160.00     2 20.333
5     2   20.0   20.0    2    2    35    3    3     2 165.00     2 23.667
6     1   18.0   17.7    2    1    64    3    3     2 172.72     1 21.000
7     2   17.7   17.7    2    1    83    3    1     2 182.88     1 18.833
8     1   17.0   17.3    2    3    74    3    1     2 157.00     2 35.833
9     2   20.0   19.5    2    3    72    3    3     2 175.00     2 19.000
10    2   18.5   18.5    2    3    90    3    3     2 167.00     2 22.333
11    1   17.0   17.2    2    1    80    3    1     2 156.20     1 28.500
12    2   21.0   21.0    2    3    68    1    1     2     NA    NA 18.250
13    1   16.0   16.0    2    1    NA    3    3     2 155.00     2 18.750
14    1   19.5   20.2    2    1    66    2    3     2 155.00     2 17.500
15    2   16.0   15.5    2    3    60    3    3     2     NA    NA 17.167
16    1   17.5   17.0    2    3    NA    3    1     2 156.00     2 17.167
17    1   18.0   18.0    2    1    89    2    1     2 157.00     2 19.333
19    2   20.5   20.5    2    1    NA    1    3     2 190.50     1 19.750
20    2   21.0   20.9    2    3    78    3    1     2 177.00     2 17.917
21    2   21.5   22.0    2    3    72    1    1     2 190.50     1 17.917
22    2   20.1   20.7    2    1    72    3    1     2 180.34     1 18.167
23    2   18.5   18.0    2    1    64    3    1     2 180.34     1 17.833
24    2   21.5   21.2    2    3    62    3    3     2 184.00     2 18.250
25    1   17.0   17.5    2    3    64    1    3     2     NA    NA 19.167
26    2   18.5   18.5    2    2    90    2    3     2     NA    NA 17.583
27    2   21.0   20.7    2    3    90    3    3     2 172.72     1 17.500
28    2   20.8   21.4    2    3    62    2    1     2 175.26     1 18.083
29    2   17.8   17.8    2    1    76    2    1     2     NA    NA 21.917
30    2   19.5   19.5    2    1    79    3    3     2 167.00     2 19.250
31    1   18.5   18.0    2    3    76    3    2     3     NA    NA 41.583
32    2   18.8   18.2    2    1    78    3    1     2 180.00     2 17.500
33    1   17.1   17.5    2    3    72    3    1     1 166.40     1 39.750
34    2   20.1   20.0    2    3    70    3    3     2 180.00     2 17.167
35    2   18.0   19.0    2    1    54    2    3     4     NA    NA 17.750
36    2   22.2   21.0    2    1    66    3    1     3 190.00     2 18.000
37    1   16.0   16.5    2    1    NA    3    3     2 168.00     2 19.000
38    2   19.4   18.5    2    3    72    2    1     2 182.50     2 17.917
39    2   22.0   22.0    2    3    80    3    3     2 185.00     2 35.500
40    2   19.0   19.0    2    3    NA    2    1     3 171.00     2 19.917
41    1   17.5   16.0    2    1    NA    3    3     2 169.00     2 17.500
42    1   17.8   18.0    2    3    72    3    3     2 154.94     1 17.083
43    2     NA     NA    2    3    60   NA    3     2 172.00     2 28.583
44    1   20.1   20.2    2    1    80    3    3     2 176.50     1 17.500
46    2   17.0   17.5    2    3    NA    2    1     2 180.34     1 18.500
47    2   23.2   22.7    2    1    84    1    1     4 180.00     2 18.917
48    2   22.5   23.0    2    3    96    3    2     2 170.00     2 19.417
49    1   18.0   17.6    2    3    60    3    3     3 168.00     2 18.417
50    1   18.0   17.9    2    3    50    1    2     2 165.00     2 30.750
52    2   20.5   20.0    2    1    68    3    1     2 190.00     2 17.500
53    2   17.0   18.0    2    1    78    1    3     2 170.18     1 18.333
54    2   20.5   19.5    2    1    56    3    1     2 179.00     2 17.417
55    2   22.5   22.5    2    3    65    3    1     4 182.00     2 20.000
56    2   18.5   18.5    2    1    NA    2    1     2 171.00     2 18.333
57    1   15.5   15.4    2    3    70    2    2     2 157.48     1 17.167
58    2   19.5   19.7    2    3    72    3    1     2     NA    NA 17.417
59    2   19.5   19.0    2    1    62    3    1     2 177.80     1 17.667
61    2   22.8   23.2    2    3    66    2    1     2 187.00     2 20.333
62    1   18.5   18.2    2    3    72    2    1     2 167.64     1 17.333
63    1   19.6   19.7    2    1    70    3    1     2 178.00     2 17.500
65    1   17.3   18.0    2    1    64    2    1     2 164.00     2 18.583
66    2   19.5   19.8    2    2    NA    3    1     2 183.00     2 18.000
67    1   19.0   19.1    2    1    NA    2    1     2 172.00     2 30.667
68    1   18.5   18.0    2    3    64    3    1     2     NA    NA 16.917
69    2   19.0   19.0    2    1    NA    3    3     2 180.00     2 19.917
70    2   21.0   19.5    2    1    80    1    2    NA     NA    NA 18.333
71    1   18.0   17.5    2    1    64    1    1     2 170.00     2 17.583
72    2   19.4   19.5    2    3    NA    3    1     1 176.00     2 17.833
73    1   17.0   16.6    2    3    68    3    3     2 171.00     2 17.667
74    1   16.5   17.0    2    1    40    1    1     2 167.64     1 17.417
75    1   15.6   15.8    2    3    88    1    3     2 165.00     2 17.750
76    1   17.5   17.5    2    2    68    3    1     1 170.00     2 20.667
77    1   17.0   17.6    2    1    76    3    3     2 165.00     2 23.583
78    1   18.6   18.0    2    1    NA    2    1     1 165.10     1 17.167
79    1   18.3   18.5    2    3    68    2    3     2 165.10     1 17.083
80    2   20.0   20.5    2    1    NA    3    1     2 185.42     1 18.750
82    2   19.2   18.9    2    3    76    3    1     2 176.50     1 20.167
83    1   17.5   17.5    2    3    98    1    1     2     NA    NA 17.667
84    1   17.0   17.4    2    3    NA    2    3     2     NA    NA 17.167
85    2   23.0   23.5    2    1    90    3    1     2 167.64     1 17.167
86    1   17.7   17.0    2    3    76    3    3     2 167.00     2 17.250
87    1   18.2   18.0    2    1    70    3    3     2 162.56     1 18.000
88    1   18.3   18.5    2    3    75    1    1     2 170.00     2 18.750
89    2   18.0   18.0    2    2    60    3    1     2 179.00     2 21.583
91    2   20.5   20.0    2    3    75    1    3     2 183.00     2 19.667
 [ reached getOption("max.print") -- omitted 135 rows ]
hand=as.data.frame(cbind(left, right))
hand=as.data.frame(cbind(left, right))

Error in data.frame(..., check.names = FALSE) : 
  arguments imply differing number of rows: 18, 218

1 个答案:

答案 0 :(得分:1)

如果要比较两个组,则将whnd留为一列,然后进行双变量分析。您可以通过多种方式执行此操作。

让我们以am中的mtcars变量为例,因为它定义了两个大小的组。让我们得到mpg的平均值。我们将使用aggregate中的stats函数,它是r基本安装的一部分。

aggregate(mpg~am, FUN= mean, data = mtcars)

  am      mpg
1  0 17.14737
2  1 24.39231

还有许多其他方法可以做到这一点,包括使用许多不同的软件包。整洁的group_bysummarize可能是一个受欢迎的选择。

library(dplyr)
mtcars %>% group_by(am) %>% summarize("Mean mpg"= mean(mpg))
# A tibble: 2 x 2
     am `Mean mpg`
  <dbl>      <dbl>
1     0       17.1
2     1       24.4