|
发表于 2012-2-8 00:20:45
|
显示全部楼层
对于楼主的问题,我确实没看明白,到底提取什么列联表。楼主可以再详细说明一下嘛
虽然不知道要提取什么信息,但是提取方法我可以做一个介绍,希望对你有用。
> data
subj sex age presentation product choice
1 1 M 21 absdiff pillow Y
2 1 M 21 absdiff table Y
3 1 M 21 absdiff helmet Y
4 1 M 21 absdiff net Y
5 2 M 19 absdiff pillow N
6 2 M 19 absdiff table N
7 2 M 19 absdiff helmet N
8 2 M 19 absdiff net N
9 3 M 19 absdiff pillow Y
10 3 M 19 absdiff table Y
11 3 M 19 absdiff helmet Y
12 3 M 19 absdiff net N
13 4 M 21 absdiff pillow Y
14 4 M 21 absdiff table Y
15 4 M 21 absdiff helmet Y
16 4 M 21 absdiff net N
17 5 M 23 absdiff pillow N
18 5 M 23 absdiff table N
##提取product中helmet的信息。
> data[data[,"product"]=="helmet",]
subj sex age presentation product choice
3 1 M 21 absdiff helmet Y
7 2 M 19 absdiff helmet N
11 3 M 19 absdiff helmet Y
15 4 M 21 absdiff helmet Y
##提取product中helmet并且choice是N的信息。
> data[data[,"product"]=="helmet"&data[,"choice"]=="N",]
subj sex age presentation product choice
7 2 M 19 absdiff helmet N
根据以上信息可做一个列联表
> Y<-unlist(sapply(c("helmet","table","pillow","net"),function(x)dim(data[data[,"product"]==x&data[,"choice"]=="Y",])[1]))
> Y
helmet table pillow net
3 3 3 1
> N<-unlist(sapply(c("helmet","table","pillow","net"),function(x)dim(data[data[,"product"]==x&data[,"choice"]=="N",])[1]))
> N
helmet table pillow net
1 2 2 3
> data.frame(Y=Y,N=N)
Y N
helmet 3 1
table 3 2
pillow 3 2
net 1 3
或者
> cbind(Y=Y,N=N)
Y N
helmet 3 1
table 3 2
pillow 3 2
net 1 3
|
|