R中循环打印输出的组合

R中循环打印输出的组合,r,for-loop,R,For Loop,我试图用降价生成一个句子来处理动态数据 ###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

我试图用降价生成一个句子来处理动态数据

###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),"%),"))
}
###生成一些示例数据

输入
a如果语句只是处理句子不同部分的大小写和标点符号。
pp
###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),"%),"))
}
###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)
a <- as.character()
for(i in 1:length(pt)) {
  if(i ==1){
    a <- c(a,   
           paste0("Type ", 
                  names(pt)[i], 
                  " accounted for ", 
                  t[i], 
                  " (",round(pt[i]*100),"%),"))
  }
  if(i < length(pt) & i > 1){
  a <- c(a,   
                          paste0("type ", 
                           names(pt)[i], 
                           " accounted for ", 
                           t[i], 
                           " (",round(pt[i]*100),"%),")
  )
  } else if (i == length(pt)){
    a <- c(a,   
           paste0("type ", 
                  names(pt)[i], 
                  " accounted for ", 
                  t[i], 
                  " (",round(pt[i]*100),"%).")
    )

  }
}

cat(a)
a <- capture.output(cat(a))