这是我的数据
org.thymeleaf.exceptions.TemplateInputException: Error resolving template "error", template might not exist or might not be accessible by any of the configured Template Resolvers
at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:870) ~[thymeleaf-3.0.8.RELEASE.jar:3.0.8.RELEASE]
at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:607) ~[thymeleaf-3.0.8.RELEASE.jar:3.0.8.RELEASE]
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098) [thymeleaf-3.0.8.RELEASE.jar:3.0.8.RELEASE]
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072) [thymeleaf-3.0.8.RELEASE.jar:3.0.8.RELEASE]
at org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:355) [thymeleaf-spring5-3.0.8.RELEASE.jar:3.0.8.RELEASE]
at org.thymeleaf.spring5.view.ThymeleafView.render(ThymeleafView.java:188) [thymeleaf-spring5-3.0.8.RELEASE.jar:3.0.8.RELEASE]
我需要输出
para1 para2 c1 c2 c3 c4 c5 c6
ast abc 3 4 NR 6 8 6
ast pqr 4 8 2 5 3 2
bc sd -0.3 2 0.4 NR NR 3
我希望通过忽略字符串NR来找到每行中从列c2到c6的列的平均值,但是它应该考虑列数,尽管NR被忽略。
答案 0 :(得分:1)
数据:
df = read.table(text='para1 para2 c1 c2 c3 c4 c5 c6
ast abc 3 4 NR 6 8 6
ast pqr 4 8 2 5 3 2
bc sd -0.3 2 0.4 NR NR 3 ',header=T)
请注意,其中包含NR
值的列被归类为factor
,而不是numeric
。
您可以使用apply
表示行:
apply(df[,c('c2','c3','c4','c5','c6')],1, function(x)
{mean(as.numeric(as.character(x)),na.rm=T)})
或者转换为矩阵并使用rowMeans
:
x<-as.matrix(df[,c('c2','c3','c4','c5','c6')])
class(x)<-'numeric'
rowMeans(x,na.rm = T)
输出:
6.0 4.0 1.8
如果您想考虑计数,如果我理解正确,您应该这样做:
apply(df[,c('c2','c3','c4','c5','c6')],1, function(x)
{sum(as.numeric(as.character(x)),na.rm=T)/length(x)})
或
x<-as.matrix(df[,c('c2','c3','c4','c5','c6')])
class(x)<-'numeric'
x[is.na(x)]=0
rowMeans(x,na.rm = T)
返回:
4.80 4.00 1.08
希望这有帮助!