先把工作目录改为文件所在目录
> d=read.csv("singers.csv")
> attributes(d)
$names
[1] "Soprano" "Alto" "Tenor" "Bass"
$class
[1] "data.frame"
$row.names
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
[25] 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
R虽已读取了所有数据,但这些数据包括NA,排列方式并不适用于分析。将d转成矩阵,再转成向量。
> part=names(d)
> part=rep(part,each=39)
> part
[1] "Soprano" "Soprano" "Soprano" "Soprano" "Soprano" "Soprano" "Soprano"
[8] "Soprano" "Soprano" "Soprano" "Soprano" "Soprano" "Soprano" "Soprano"
[15] "Soprano" "Soprano" "Soprano" "Soprano" "Soprano" "Soprano" "Soprano"
[22] "Soprano" "Soprano" "Soprano" "Soprano" "Soprano" "Soprano" "Soprano"
[29] "Soprano" "Soprano" "Soprano" "Soprano" "Soprano" "Soprano" "Soprano"
[36] "Soprano" "Soprano" "Soprano" "Soprano" "Alto" "Alto" "Alto"
[43] "Alto" "Alto" "Alto" "Alto" "Alto" "Alto" "Alto"
[50] "Alto" "Alto" "Alto" "Alto" "Alto" "Alto" "Alto"
[57] "Alto" "Alto" "Alto" "Alto" "Alto" "Alto" "Alto"
[64] "Alto" "Alto" "Alto" "Alto" "Alto" "Alto" "Alto"
[71] "Alto" "Alto" "Alto" "Alto" "Alto" "Alto" "Alto"
[78] "Alto" "Tenor" "Tenor" "Tenor" "Tenor" "Tenor" "Tenor"
[85] "Tenor" "Tenor" "Tenor" "Tenor" "Tenor" "Tenor" "Tenor"
[92] "Tenor" "Tenor" "Tenor" "Tenor" "Tenor" "Tenor" "Tenor"
[99] "Tenor" "Tenor" "Tenor" "Tenor" "Tenor" "Tenor" "Tenor"
[106] "Tenor" "Tenor" "Tenor" "Tenor" "Tenor" "Tenor" "Tenor"
[113] "Tenor" "Tenor" "Tenor" "Tenor" "Tenor" "Bass" "Bass"
[120] "Bass" "Bass" "Bass" "Bass" "Bass" "Bass" "Bass"
[127] "Bass" "Bass" "Bass" "Bass" "Bass" "Bass" "Bass"
[134] "Bass" "Bass" "Bass" "Bass" "Bass" "Bass" "Bass"
[141] "Bass" "Bass" "Bass" "Bass" "Bass" "Bass" "Bass"
[148] "Bass" "Bass" "Bass" "Bass" "Bass" "Bass" "Bass"
> temp=matrix(c(d,part),nc=2)
> temp
[,1] [,2]
[1,] "64" "Soprano"
[2,] "62" "Soprano"
[3,] "66" "Soprano"
[4,] "65" "Soprano"
[5,] "60" "Soprano"
[6,] "61" "Soprano"
[7,] "65" "Soprano"
[8,] "66" "Soprano"
[9,] "65" "Soprano"
[10,] "63" "Soprano"
[11,] "67" "Soprano"
[12,] "65" "Soprano"
[13,] "62" "Soprano"
[14,] "65" "Soprano"
[15,] "68" "Soprano"
[16,] "65" "Soprano"
[17,] "63" "Soprano"
[18,] "65" "Soprano"
[19,] "62" "Soprano"
[20,] "65" "Soprano"
[21,] "66" "Soprano"
[22,] "62" "Soprano"
[23,] "65" "Soprano"
[24,] "63" "Soprano"
[25,] "65" "Soprano"
[26,] "66" "Soprano"
[27,] "65" "Soprano"
[28,] "62" "Soprano"
[29,] "65" "Soprano"
[30,] "66" "Soprano"
[31,] "65" "Soprano"
[32,] "61" "Soprano"
[33,] "65" "Soprano"
[34,] "66" "Soprano"
[35,] "65" "Soprano"
[36,] "62" "Soprano"
[37,] NA "Soprano"
[38,] NA "Soprano"
[39,] NA "Soprano"
[40,] "65" "Alto"
[41,] "62" "Alto"
[42,] "68" "Alto"
[43,] "67" "Alto"
[44,] "67" "Alto"
[45,] "63" "Alto"
[46,] "67" "Alto"
[47,] "66" "Alto"
[48,] "63" "Alto"
[49,] "72" "Alto"
[50,] "62" "Alto"
[51,] "61" "Alto"
[52,] "66" "Alto"
[53,] "64" "Alto"
[54,] "60" "Alto"
[55,] "61" "Alto"
[56,] "66" "Alto"
[57,] "66" "Alto"
[58,] "66" "Alto"
[59,] "62" "Alto"
[60,] "70" "Alto"
[61,] "65" "Alto"
[62,] "64" "Alto"
[63,] "63" "Alto"
[64,] "65" "Alto"
[65,] "69" "Alto"
[66,] "61" "Alto"
[67,] "66" "Alto"
[68,] "65" "Alto"
[69,] "61" "Alto"
[70,] "63" "Alto"
[71,] "64" "Alto"
[72,] "67" "Alto"
[73,] "66" "Alto"
[74,] "68" "Alto"
[75,] NA "Alto"
[76,] NA "Alto"
[77,] NA "Alto"
[78,] NA "Alto"
[79,] "69" "Tenor"
[80,] "72" "Tenor"
[81,] "71" "Tenor"
[82,] "66" "Tenor"
[83,] "76" "Tenor"
[84,] "74" "Tenor"
[85,] "71" "Tenor"
[86,] "66" "Tenor"
[87,] "68" "Tenor"
[88,] "67" "Tenor"
[89,] "70" "Tenor"
[90,] "65" "Tenor"
[91,] "72" "Tenor"
[92,] "70" "Tenor"
[93,] "68" "Tenor"
[94,] "73" "Tenor"
[95,] "66" "Tenor"
[96,] "68" "Tenor"
[97,] "67" "Tenor"
[98,] "64" "Tenor"
[99,] NA "Tenor"
[100,] NA "Tenor"
[101,] NA "Tenor"
[102,] NA "Tenor"
[103,] NA "Tenor"
[104,] NA "Tenor"
[105,] NA "Tenor"
[106,] NA "Tenor"
[107,] NA "Tenor"
[108,] NA "Tenor"
[109,] NA "Tenor"
[110,] NA "Tenor"
[111,] NA "Tenor"
[112,] NA "Tenor"
[113,] NA "Tenor"
[114,] NA "Tenor"
[115,] NA "Tenor"
[116,] NA "Tenor"
[117,] NA "Tenor"
[118,] "72" "Bass"
[119,] "70" "Bass"
[120,] "72" "Bass"
[121,] "69" "Bass"
[122,] "73" "Bass"
[123,] "71" "Bass"
[124,] "72" "Bass"
[125,] "68" "Bass"
[126,] "68" "Bass"
[127,] "71" "Bass"
[128,] "66" "Bass"
[129,] "68" "Bass"
[130,] "71" "Bass"
[131,] "73" "Bass"
[132,] "73" "Bass"
[133,] "70" "Bass"
[134,] "68" "Bass"
[135,] "70" "Bass"
[136,] "75" "Bass"
[137,] "68" "Bass"
[138,] "71" "Bass"
[139,] "70" "Bass"
[140,] "74" "Bass"
[141,] "70" "Bass"
[142,] "75" "Bass"
[143,] "75" "Bass"
[144,] "69" "Bass"
[145,] "72" "Bass"
[146,] "71" "Bass"
[147,] "70" "Bass"
[148,] "71" "Bass"
[149,] "68" "Bass"
[150,] "70" "Bass"
[151,] "75" "Bass"
[152,] "72" "Bass"
[153,] "66" "Bass"
[154,] "72" "Bass"
[155,] "70" "Bass"
[156,] "69" "Bass"
选取在temp中不是NA的行,然后把它转化为数据框Frame
> temp=temp[!is.na(temp[,1]),]
> temp
[,1] [,2]
[1,] "64" "Soprano"
[2,] "62" "Soprano"
[3,] "66" "Soprano"
[4,] "65" "Soprano"
[5,] "60" "Soprano"
[6,] "61" "Soprano"
[7,] "65" "Soprano"
[8,] "66" "Soprano"
[9,] "65" "Soprano"
[10,] "63" "Soprano"
[11,] "67" "Soprano"
[12,] "65" "Soprano"
[13,] "62" "Soprano"
[14,] "65" "Soprano"
[15,] "68" "Soprano"
[16,] "65" "Soprano"
[17,] "63" "Soprano"
[18,] "65" "Soprano"
[19,] "62" "Soprano"
[20,] "65" "Soprano"
[21,] "66" "Soprano"
[22,] "62" "Soprano"
[23,] "65" "Soprano"
[24,] "63" "Soprano"
[25,] "65" "Soprano"
[26,] "66" "Soprano"
[27,] "65" "Soprano"
[28,] "62" "Soprano"
[29,] "65" "Soprano"
[30,] "66" "Soprano"
[31,] "65" "Soprano"
[32,] "61" "Soprano"
[33,] "65" "Soprano"
[34,] "66" "Soprano"
[35,] "65" "Soprano"
[36,] "62" "Soprano"
[37,] "65" "Alto"
[38,] "62" "Alto"
[39,] "68" "Alto"
[40,] "67" "Alto"
[41,] "67" "Alto"
[42,] "63" "Alto"
[43,] "67" "Alto"
[44,] "66" "Alto"
[45,] "63" "Alto"
[46,] "72" "Alto"
[47,] "62" "Alto"
[48,] "61" "Alto"
[49,] "66" "Alto"
[50,] "64" "Alto"
[51,] "60" "Alto"
[52,] "61" "Alto"
[53,] "66" "Alto"
[54,] "66" "Alto"
[55,] "66" "Alto"
[56,] "62" "Alto"
[57,] "70" "Alto"
[58,] "65" "Alto"
[59,] "64" "Alto"
[60,] "63" "Alto"
[61,] "65" "Alto"
[62,] "69" "Alto"
[63,] "61" "Alto"
[64,] "66" "Alto"
[65,] "65" "Alto"
[66,] "61" "Alto"
[67,] "63" "Alto"
[68,] "64" "Alto"
[69,] "67" "Alto"
[70,] "66" "Alto"
[71,] "68" "Alto"
[72,] "69" "Tenor"
[73,] "72" "Tenor"
[74,] "71" "Tenor"
[75,] "66" "Tenor"
[76,] "76" "Tenor"
[77,] "74" "Tenor"
[78,] "71" "Tenor"
[79,] "66" "Tenor"
[80,] "68" "Tenor"
[81,] "67" "Tenor"
[82,] "70" "Tenor"
[83,] "65" "Tenor"
[84,] "72" "Tenor"
[85,] "70" "Tenor"
[86,] "68" "Tenor"
[87,] "73" "Tenor"
[88,] "66" "Tenor"
[89,] "68" "Tenor"
[90,] "67" "Tenor"
[91,] "64" "Tenor"
[92,] "72" "Bass"
[93,] "70" "Bass"
[94,] "72" "Bass"
[95,] "69" "Bass"
[96,] "73" "Bass"
[97,] "71" "Bass"
[98,] "72" "Bass"
[99,] "68" "Bass"
[100,] "68" "Bass"
[101,] "71" "Bass"
[102,] "66" "Bass"
[103,] "68" "Bass"
[104,] "71" "Bass"
[105,] "73" "Bass"
[106,] "73" "Bass"
[107,] "70" "Bass"
[108,] "68" "Bass"
[109,] "70" "Bass"
[110,] "75" "Bass"
[111,] "68" "Bass"
[112,] "71" "Bass"
[113,] "70" "Bass"
[114,] "74" "Bass"
[115,] "70" "Bass"
[116,] "75" "Bass"
[117,] "75" "Bass"
[118,] "69" "Bass"
[119,] "72" "Bass"
[120,] "71" "Bass"
[121,] "70" "Bass"
[122,] "71" "Bass"
[123,] "68" "Bass"
[124,] "70" "Bass"
[125,] "75" "Bass"
[126,] "72" "Bass"
[127,] "66" "Bass"
[128,] "72" "Bass"
[129,] "70" "Bass"
[130,] "69" "Bass"
> singers=data.frame(temp)
> singers
X1 X2
1 64 Soprano
2 62 Soprano
3 66 Soprano
4 65 Soprano
5 60 Soprano
6 61 Soprano
7 65 Soprano
8 66 Soprano
9 65 Soprano
10 63 Soprano
11 67 Soprano
12 65 Soprano
13 62 Soprano
14 65 Soprano
15 68 Soprano
16 65 Soprano
17 63 Soprano
18 65 Soprano
19 62 Soprano
20 65 Soprano
21 66 Soprano
22 62 Soprano
23 65 Soprano
24 63 Soprano
25 65 Soprano
26 66 Soprano
27 65 Soprano
28 62 Soprano
29 65 Soprano
30 66 Soprano
31 65 Soprano
32 61 Soprano
33 65 Soprano
34 66 Soprano
35 65 Soprano
36 62 Soprano
37 65 Alto
38 62 Alto
39 68 Alto
40 67 Alto
41 67 Alto
42 63 Alto
43 67 Alto
44 66 Alto
45 63 Alto
46 72 Alto
47 62 Alto
48 61 Alto
49 66 Alto
50 64 Alto
51 60 Alto
52 61 Alto
53 66 Alto
54 66 Alto
55 66 Alto
56 62 Alto
57 70 Alto
58 65 Alto
59 64 Alto
60 63 Alto
61 65 Alto
62 69 Alto
63 61 Alto
64 66 Alto
65 65 Alto
66 61 Alto
67 63 Alto
68 64 Alto
69 67 Alto
70 66 Alto
71 68 Alto
72 69 Tenor
73 72 Tenor
74 71 Tenor
75 66 Tenor
76 76 Tenor
77 74 Tenor
78 71 Tenor
79 66 Tenor
80 68 Tenor
81 67 Tenor
82 70 Tenor
83 65 Tenor
84 72 Tenor
85 70 Tenor
86 68 Tenor
87 73 Tenor
88 66 Tenor
89 68 Tenor
90 67 Tenor
91 64 Tenor
92 72 Bass
93 70 Bass
94 72 Bass
95 69 Bass
96 73 Bass
97 71 Bass
98 72 Bass
99 68 Bass
100 68 Bass
101 71 Bass
102 66 Bass
103 68 Bass
104 71 Bass
105 73 Bass
106 73 Bass
107 70 Bass
108 68 Bass
109 70 Bass
110 75 Bass
111 68 Bass
112 71 Bass
113 70 Bass
114 74 Bass
115 70 Bass
116 75 Bass
117 75 Bass
118 69 Bass
119 72 Bass
120 71 Bass
121 70 Bass
122 71 Bass
123 68 Bass
124 70 Bass
125 75 Bass
126 72 Bass
127 66 Bass
128 72 Bass
129 70 Bass
130 69 Bass
> names(singers)=c("Height","Part")
> singers $ Height=as.numeric(singers $ Height)
> singers $ Part=ordered(as.factor(singers $ Part),levels=c("Bass","Tenor","Alto","Soprano"))
> singers
Height Part
1 5 Soprano
2 3 Soprano
3 7 Soprano
4 6 Soprano
5 1 Soprano
6 2 Soprano
7 6 Soprano
8 7 Soprano
9 6 Soprano
10 4 Soprano
11 8 Soprano
12 6 Soprano
13 3 Soprano
14 6 Soprano
15 9 Soprano
16 6 Soprano
17 4 Soprano
18 6 Soprano
19 3 Soprano
20 6 Soprano
21 7 Soprano
22 3 Soprano
23 6 Soprano
24 4 Soprano
25 6 Soprano
26 7 Soprano
27 6 Soprano
28 3 Soprano
29 6 Soprano
30 7 Soprano
31 6 Soprano
32 2 Soprano
33 6 Soprano
34 7 Soprano
35 6 Soprano
36 3 Soprano
37 6 Alto
38 3 Alto
39 9 Alto
40 8 Alto
41 8 Alto
42 4 Alto
43 8 Alto
44 7 Alto
45 4 Alto
46 13 Alto
47 3 Alto
48 2 Alto
49 7 Alto
50 5 Alto
51 1 Alto
52 2 Alto
53 7 Alto
54 7 Alto
55 7 Alto
56 3 Alto
57 11 Alto
58 6 Alto
59 5 Alto
60 4 Alto
61 6 Alto
62 10 Alto
63 2 Alto
64 7 Alto
65 6 Alto
66 2 Alto
67 4 Alto
68 5 Alto
69 8 Alto
70 7 Alto
71 9 Alto
72 10 Tenor
73 13 Tenor
74 12 Tenor
75 7 Tenor
76 17 Tenor
77 15 Tenor
78 12 Tenor
79 7 Tenor
80 9 Tenor
81 8 Tenor
82 11 Tenor
83 6 Tenor
84 13 Tenor
85 11 Tenor
86 9 Tenor
87 14 Tenor
88 7 Tenor
89 9 Tenor
90 8 Tenor
91 5 Tenor
92 13 Bass
93 11 Bass
94 13 Bass
95 10 Bass
96 14 Bass
97 12 Bass
98 13 Bass
99 9 Bass
100 9 Bass
101 12 Bass
102 7 Bass
103 9 Bass
104 12 Bass
105 14 Bass
106 14 Bass
107 11 Bass
108 9 Bass
109 11 Bass
110 16 Bass
111 9 Bass
112 12 Bass
113 11 Bass
114 15 Bass
115 11 Bass
116 16 Bass
117 16 Bass
118 10 Bass
119 13 Bass
120 12 Bass
121 11 Bass
122 12 Bass
123 9 Bass
124 11 Bass
125 16 Bass
126 13 Bass
127 7 Bass
128 13 Bass
129 11 Bass
130 10 Bass
singers就转成了我们想要的Frame
但,singers $ Height=as.numeric(singers $ Height),像是把原来的数当成了ASC码,转成numeric时数据的值发生了变化,这可能对统计结果不产生影响,可似乎还是不大正确。