2010-09-02 57 views
0

我有想貓不同的字符串來使用,如果在不同的條件文件 - else語句:[R語法問題

cat("<graph caption=\"ECG Data Wave\" subcaption=\"For Person's Name\" xAxisName=\"Time\" yAxisMinValue=\"-0.025\" yAxisName=\"Voltage\" decimalPrecision=\"5\" formatNumberScale=\"0\" numberPrefix=\"\" showNames=\"1\" showValues=\"0\" showAlternateHGridColor=\"1\" AlternateHGridColor=\"ff5904\" divLineColor=\"ff5904\" divLineAlpha=\"20\" alternateHGridAlpha=\"5\">\n") 
    if (df$BP > 140 && df$Yvoltage < -0.05){ 
    cat(sprintf(" <set name=\"%s\" value=\"%f\" hoverText = \"Blood Pressure High, Level of Activity Low\" ></set>\n", df$XTimeStamp, df$YVoltage, diff))else 
     if (df$BP > 140 && df$Yvoltage > -0.05) 
      cat(sprintf(" <set name=\"%s\" value=\"%f\" hoverText = \"Blood Pressure Normal, Level of Activity Normal\" ></set>\n", df$XTimeStamp, df$YVoltage, diff)) else 
       if (df$BP > 140 && df$Yvoltage > -0.35) 
        cat(sprintf(" <set name=\"%s\" value=\"%f\" hoverText = \"Blood Pressure High, Level of Activity High\" ></set>\n", df$XTimeStamp, df$YVoltage, diff))} 
    cat(' <trendlines> 
     <line startvalue="0.30" displayValue="High Activity" color="FF0000" thickness="1" isTrendZone="0"></line> 
     <line startvalue="-0.05" displayValue="Low Activity" color="009999" thickness="1" isTrendZone="0"></line> 

\ n「) 貓( 」\ n「)

當我試圖運行我不斷收到錯誤的代碼,如下所示:

Error: unexpected 'else' in: 
"if (df$BP > 140 && df$Yvoltage < -0.05){ 
cat(sprintf(" <set name=\"%s\" value=\"%f\" hoverText = \"Blood Pressure High, Level of Activity Low\" ></set>\n", df$XTimeStamp, df$YVoltage, diff))else" 

任何想法,我哪裏會出錯?這是否正確,這是否有更好的方法來編寫文件?

我正在嘗試關注Spacedman的建議並使用brew。我曾嘗試它不同的差異和我要麼收到以下錯誤信息:

Error: unexpected input in "cat (sprintf(" <set name=\"%s\" value=\"%f\" hoverText = "<%= hovertext =>" ></set>\n", df$XTimeStamp, df$YVoltage))" 

,或者如果我得到它的工作hovertext保持不變,整個一路下滑。

目前將R代碼如下:

if(df$BP > 140 && df$YVoltage < -0.05){ 
    hovertext ="Blood Pressure High, Level of Activity Low" 
}else{ 
    if(df$BP < 140 && df$BP > 90 && df$YVoltage > -0.05 && df$YVoltage < 0.30){ 
    hovertext ="Blood Pressure normal, Level of Activity normal" 
     } else { 
    hovertext ="Blood Pressure High, Level of Activity high" 
     } 

sink (file="c:/Users/usr/Desktop/data.xml", type="output",split=FALSE) 
cat("<graph caption=\"ECG Data Wave\" subcaption=\"For Person's Name\" xAxisName=\"Time\" yAxisMinValue=\"-0.025\" yAxisName=\"Voltage\" decimalPrecision=\"5\" formatNumberScale=\"0\" numberPrefix=\"\" showNames=\"1\" showValues=\"0\" showAlternateHGridColor=\"1\" AlternateHGridColor=\"ff5904\" divLineColor=\"ff5904\" divLineAlpha=\"20\" alternateHGridAlpha=\"5\">\n") 
cat(sprintf(" <set name=\"%s\" value=\"%f\" hoverText = "<%= hovertext =>" ></set>\n", df$XTimeStamp, df$YVoltage)) 
cat(' <trendlines> 
    <line startvalue="0.30" displayValue="High Activity" color="FF0000" thickness="1" isTrendZone="0"></line> 
    <line startvalue="-0.05" displayValue="Low Activity" color="009999" thickness="1" isTrendZone="0"></line> 
</trendlines>\n') 
cat ("</graph>\n") 
unlink("data.xml") 

在哪裏我仍然在代碼中走錯了任何的想法?

+0

您似乎沒有指定要寫入的文件,只會打印出原樣。 – James 2010-09-02 09:23:07

回答

2

沒有收尾支架(})之前else

對於嵌套if,你應該使用:

if (test) {statement} else {if (test) {statement} else {...}} 

而且,你似乎在你if語句中引用data.frame列。如果nrow> 1,則只使用第一個元素。對於矢量化版本,請使用ifelse,但我不確定這會完全符合您的要求,您可能會寫入一個循環。

+0

將'length> 1'更改爲'nrow> 1'。對於數據框,length返回列數,但if語句在有多行時出錯。 – 2010-09-02 13:45:53

+0

@Richie:謝謝,我正在引用數據框的列,但可以看到這個不清楚。 – James 2010-09-02 14:25:11

1

另外,還有代碼。嘗試使用「brew」包裝(在CRAN上)進行模板化。它使事情變得簡單,爲模板字符串中的引號節省了大量的反斜槓,並強制您從模板字符串中取出大量邏輯,並將其放入屬於它的應用程序中。

例如,您的第一個if-else混亂只是決定使用哪個hovertext。這將是更好,因爲:

if(foo){ 
    hovertext = "first hover text" 
}else{ 
    if(foobar){ 
     hovertext = "second hovertext" 
    } else { 
    hovertext = "third hovertext" 
    } 
} 

在您的BREW模板文件然後把hovertext:

... 
<set name="<%= df$XTimeStamp %>" value="<%= df$YVoltage=>" hoverText = "<%= hovertext =>" ></set> 
... 

而且也,你sprintfs有兩個佔位符(%-markers),但三個變量。最後的額外'差異'是什麼?使用brew的另一個原因!

+0

感謝您的建議釀造。我已編輯的代碼如下: 如果(DF $ BP> 140 && DF $ YVoltage <-0.05){ hovertext = 「血壓高,低活動水平」 }否則{如果 (DF $ BP < 140 && DF $ BP> 90 && DF $ YVoltage> -0.05 && DF $ YVoltage <0.30){ hovertext = 「血壓正常,正常活動級別」 }否則{ hovertext = 「高血壓」 } } cat(sprintf(「「> \ n」,df $ XTimeStamp,df $ YVoltage)) 我現在得到以下內容:警告消息: 1:在Ops.factor(df $ BP,140)中:>對因素 – 2010-09-02 11:33:35

+0

排序無意義因素錯誤消息。還處理了以下字符串:cat(sprintf(「 \「> \ n」 XTimeStamp,df $ YVoltage))但是當我運行它時,我收到以下錯誤消息: sprintf錯誤(「 \「> \ n」,:: 參數太少 – 2010-09-02 14:51:09

0

更好地使用XML包將您的數據轉換爲XML。然而,詹姆斯發現了這個問題本身。