2012-09-12 38 views
0

我認爲摘要函數R可能需要字符變量給定它指向的文件是數字。 bbb給了我很好的結果,但aaa的總結給我帶來了問題。如何在摘要函數中插入字符串R

# Descriptives For Machine.x 
for (n in 1:3500){ 
    machine_name = paste("machine.wt$Machine.", n, sep="") 
    aaa = summary (paste("machine.wt$Machine.", n, sep="")) 
    bbb = summary (machine.wt$Machine.1) 
} 

Error in x - mx : non-numeric argument to binary operator 
In addition: Warning messages: 
1: In mean.default(x, na.rm = na.rm) : 
    argument is not numeric or logical: returning NA 
2: In var(x, na.rm = na.rm) : NAs introduced by coercion 
3: In mean.default(x, na.rm = na.rm, trim = trim) : 
    argument is not numeric or logical: returning NA 
4: In var(x, na.rm = na.rm) : NAs introduced by coercion 
5: In mean.default(x) : argument is not numeric or logical: returning NA 

我的數據是非常簡單的:

Obs.1 Machine.1 Obs.1 Machine.2 Obs.2 Machine.3 Obs.3 Machine.4 
1 302.3 1 302.8 1 315.1 1 300.9 
2 295.3 2 314.3 2 306.7 2 317.5 
3 301.4 3 308.4 3 309.0 3 304.3 
4 318.2 4 295.2 4 321.3 4 293.2 
5 320.4 5 313.5 5 326.4 5 308.0 

回答

1

如何使用subsetgrepl完全避免的循環?

summary(subset(machine.wt,select=grepl("^Machine",names(machine.wt)))) 
+0

我喜歡這兩個答案,但我認爲我必須去這個,因爲它彎曲了我的大腦。 – oaxacamatt

0

fortune(312)然後嘗試

summary(machine.wt[[ paste0('Machine.',n) ]]) 
+0

這兩個工作都很好。 – oaxacamatt

相關問題