R "\"字符 names rownames,colnames,Table
2011-06-14 21:25:23| 分类:
R&Bioconductor
| 标签:
|举报
|字号大中小 订阅
The backslash
(\) is known as the escape character. In a similar vein, you can insert quote
characters with \", as in
>cat("What is \"R\"?\n")
What is "R"?
> x <- c(red="Huey", blue="Dewey", green="Louie")
> x
red blue green
"Huey" "Dewey" "Louie"
> names(x)
[1] "red" "blue" "green"
> caff.marital <- matrix(c(652,1537,598,242,36,46,38,21,218
+ ,327,106,67),
+ nrow=3,byrow=T)
> caff.marital
[,1] [,2] [,3] [,4]
[1,] 652 1537 598 242
[2,] 36 46 38 21
[3,] 218 327 106 67
> colnames(caff.marital) <- c("0","1-150","151-300",">300")
> rownames(caff.marital) <- c("Married","Prev.married","Single")
> caff.marital
0 1-150 151-300 >300
Married 652 1537 598 242
Prev.married 36 46 38 21
Single 218 327 106 67
> names(dimnames(caff.marital)) <- c("marital","consumption")
> caff.marital
consumption
marital 0 1-150 151-300 >300
Married 652 1537 598 242
Prev.married 36 46 38 21
Single 218 327 106 67
you can convert to that matrices class using as.table(caff.marital) from "table".In that case, you should do the
tabulation with table, xtabs, or ftable
> as.data.frame(as.table(caff.marital))
marital consumption Freq
1 Married 0 652
2 Prev.married 0 36
3 Single 0 218
4 Married 1-150 1537
5 Prev.married 1-150 46
6 Single 1-150 327
7 Married 151-300 598
8 Prev.married 151-300 38
9 Single 151-300 106
10 Married >300 242
11 Prev.married >300 21
12 Single >300 67
margin.table(行求和)
> total.caff <- margin.table(caff.marital,2)
> total.caff
consumption
0 1-150 151-300 >300
906 1910 742 330
prop.table(caff.marital)
BARPLOT图
> barplot(total.caff, col="white")
> par(mfrow=c(2,2))
> barplot(caff.marital, col="white")
> barplot(t(caff.marital), col="white")
#If you want to place the row contributions beside each other instead, you can use the argument beside=T
> barplot(t(caff.marital), col="white", beside=T)
> barplot(prop.table(t(caff.marital),2), col="white", beside=T)
> par(mfrow=c(1,1))
>barplot(prop.table(t(caff.marital),2),beside=T,
+ legend.text=colnames(caff.marital),
+ col=c("white","grey80","grey50","black"))
DOTCHART图
>dotchart(t(caff.marital), lcolor="black")
PIE图
> opar <- par(mfrow=c(2,2),mex=0.8, mar=c(1,1,2,1))
> slices <- c("white","grey80","grey50","black")
> pie(caff.marital["Married",], main="Married", col=slices)
> pie(caff.marital["Prev.married",],
+ main="Previously married", col=slices)
> pie(caff.marital["Single",], main="Single", col=slices)
> par(opar)
评论这张
转发至微博
转发至微博
评论