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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| A<-sample(1:5,10,replace=TRUE)
B<-sample(1:3,10,replace=TRUE)
C<-sample(1:6,10,replace=TRUE)
M<-data.frame(A,B,C)
is.factor(M[,1])
[1] FALSE
summary(M)
A B C
Min. :1.00 Min. :1.00 Min. :1.0
1st Qu.:2.25 1st Qu.:1.25 1st Qu.:2.0
Median :4.50 Median :2.00 Median :5.0
Mean :3.70 Mean :2.10 Mean :4.1
3rd Qu.:5.00 3rd Qu.:3.00 3rd Qu.:6.0
Max. :5.00 Max. :3.00 Max. :6.0
## test 1 ##
M<-apply(M,2,as.factor)
is.factor(M[,1])
[1] FALSE
summary(M)
A B C
1:1 1:3 1:1
2:2 2:3 2:3
3:1 3:4 5:2
4:1 6:4
5:5
## test 2 ##
M<-data.frame(A,B,C)
for(k in 1:ncol(M))
{
M[,k]<-as.factor(M[,k])
}
is.factor(M[,1])
[1] TRUE
summary(M)
A B C
1:1 1:3 1:1
2:2 2:3 2:3
3:1 3:4 5:2
4:1 6:4
5:5 |
Partager