我试图产生降价的句子来处理动态数据结合for循环打印输出中的R
###Generate some sample data
Type <- c("A","A","A","A","A","A","A","A","A",
"B","B","B","B","B","B","B","B",
"C","C","C","C","C","C","C",
"ABC","ABC","ABC","ABC","ABC")
Type <- as.data.frame(Type)
###Set the tables and iterations
l <- length(unique(Type$Type))
t <- table(as.character(Type$Type))
pt <- prop.table((table(as.character(Type$Type))))
###Loop to print the first type in sentence
for(i in seq(from=1, to=1)) {
typebegin <- print(paste0("Type ",
names(pt)[i],
" accounted for ",
t[i],
" (",round(pt[i]*100),"%),"))
}
这里是哪里出了问题:
###Loop to print all the types in the middle
for(i in seq(from=2, to=(l-1),by=1)) {
typemid <- print(paste0("type ",
names(pt)[i],
" accounted for ",
t[i],
" (",round(pt[i]*100),"%),"))
}
我从一开始的输出功能为:
[1] “键入ABC占5(17%),”
[1]“B型占为8(28%),“
我不知道如何连接这些。
###Loop to end the sentence
for(i in seq(from=l, to=l)) {
typeend <- print(paste0("type ",
names(pt)[i],
" accounted for ",
t[i],
" (",round(pt[i]*100),"%)."))
}
###Print the sentence
paste(typebegin, typemid, typeend)
[1] “C型占7(24%),B型占8(28%),C型占7(24%)”。甲
'PP < - 表(类型); pp < - 矩阵(c(名称(pp),pp,round(prop.table(pp)* 100)),ncol = 3); (<%s占%s(%s %%)',x [1],x [2],x [3])); sub('(。)','\\ U \\ 1',paste(pr,collapse =','),perl = TRUE)' – rawr
pp < - table(Type); (c(名称(pp),pp,round(prop.table(pp)* 100)),ncol = 3); (x,sprintf,'type%s占%s(%s %%)',x [1],x [2],x [3])); pr < - paste0(sub('(。)','\\ U \\ 1',paste(pr,collapse =','),perl = TRUE),“。”) pr – SCDCE