在“count”变量中,我想获得在R
中调用函数的次数FUN=function(x){
spaces=Category=0
k=50-nchar(levels(x))
for (z in 1:length(levels(x)) )
{
spaces[z]=paste(replicate(k[z]," "), collapse="")
Category[z]=paste(levels(x)[z],spaces[z],sep="")
}
df=as.data.frame.matrix(table(x,Sentiment))
attach(df)
tot=d+i+s;
d_percent=d/tot
i_percent=i/tot
s_percent=s/tot
HSI=(round(100+(i_percent-d_percent)*(1-s_percent)*100))
Weightage=(round(tot*100/sum(d+i+s),1))
HSI_Final=cbind(count,Category,HSI,Weightage)
return(HSI_Final)
}
答案 0 :(得分:3)
你可以做这样的事情
FUN <- local({count=1; function(x){
HSI_Final=count
count <<- count + 1
return(HSI_Final)
}})
FUN(1)
# [1] 1
FUN(1)
# [1] 2
FUN(1)
# [1] 3
FUN(1)
# [1] 4
在这里创建一个本地环境,其中包含可以在函数
中递增的计数变量