我有一个使用此生成的数据集
set.seed(1221)
runs<-2
pd<-list()
for(k in 1:runs){
p<-10
n_b <- sample(1:6,p,replace = T)
bs<-c("S","M","L")
cl <- sample(1:3,p,replace= T)
Y<-4
d_km<-5.5
cdata<-as.data.frame(matrix(0,p,20))
for(i in 1:nrow(cdata)){
cdata[i,1]<-n_b[i]
for (j in 2:(n_b[i]+1)){
cdata[i,j] <- rnorm(1,9,2)
}
cdata[i,8]<- sum(cdata[i,2:7])
cdata[i,9]<-cl[i]
if (cdata[i,9]==1){
cdata[i,10]=max(0,cdata[i,8]-23)
}
else if (cdata[i,9]==2){
cdata[i,10]=max(0,cdata[i,8]-28)
}
else{
cdata[i,10]=max(0,cdata[i,8]-32)
}
cdata [i,11]= cdata[i,10]*Y*d_km
if (cdata[i,9]==1){
cdata[i,12]=max(0,cdata[i,1]-2)
}
else if (cdata[i,9]==2) {
cdata[i,12]=max(0,cdata[i,1]-2)
}
else {
cdata[i,12]=max(0,cdata[i,1]-3)
}
if (cdata[i,12]>=1){
cdata[i,13]=sample(bs,1,replace = F)
}
else{
0
}
if (cdata[i,12]>=2){
cdata[i,14]=sample(bs,1,replace = F)
}
else{
0
}
if (cdata[i,12]>=3){
cdata[i,15]=sample(bs,1,replace = F)
}
else{
0
}
if (cdata[i,12]>=4){
cdata[i,16]=sample(bs,1,replace = F)
}
else{
0
}
for(j in 13:16){
if(cdata[i,j]=="S"){
cdata[i,j+4]=runif(1,115,125)
}else if(cdata[i,j]=="M"){
cdata[i,j+4]=runif(1,135,145)
}else if(cdata[i,j]=="L"){
cdata[i,j+4]=runif(1,145,158)
} else {
cdata[i,j+4]=0
}
}
}
paxd<-cdata[,c(9,13:16)]
pd[[k]]<-paxd
}
pdf<-as.data.frame(do.call(rbind,pd))
pdff<-cbind(day=rep(1:runs,each=p),pdf)
I need to formulate table like this shown in the figure attached. . \
The " S", "M", and "L" column will show the calculated number of "S", "M" and "L" for each day and for each class. Basically a count.
I tried this to filter the data using dplyr
colnames(pdff)<-c("day","class","B-1","B-2","B-3","B-4")
pdfx<-pdff %>% filter(day==1) %>% filter(class==1)
之后,我被卡住了。谁能帮忙使用数据分析?